I want to inject some method calls to Nullable<T>.HasValue in my assembly 
(which is a PCL). But after injecting the call to HasValue, 
'AssemblyDefinition.Write' says I need to import Nullable<T>.HasValue 
because it isn't in the module I'm trying to rewrite. It's definitely in my 
assembly because I call it elsewhere at compile time. So I'm assuming the 
reference I'm getting to Nullable<T>.HasValue is from some other version of 
.NET, and it doesn't like that. If I do the import, then it adds 
AssemblyReferences to System and mscorlib for the full .NET 4.5. Since my 
assembly is a PCL, this doesn't work at all when actually running it in 
some other environment.

Code sample:

*var field = ...; // a FieldDefinition to Nullable<DateTime> that I want to 
make calls to*
*var get_HasValue = field.FieldType.Resolve().Methods(i => i.Name == 
"get_HasValue");*
*...*
*il.Add(Instruction.Create(OpCodes.Call, get_HasValue));*
*...*
*myAssembly.Write();*


With the import that breaks PCLs:
*...*

*var get_HasValue = 
myAssembly.MainModule.Import(field.FieldType.Resolve().Methods(i => i.Name 
== "get_HasValue"));*
*...*

Does this have something to do with the fact that I am calling 
xx.Resolve()? How can I get a 'MethodReference' to some method on some type 
in a ReferencedAssembly without having to import something?
I can look through the IL and find some existing call to 'HasValue' and 
just use that MethodReference and it works, but how can I do this in a 
repeatable way? In general, I want to be able to find types and method 
references to referenced assemblies. And doing an import this way breaks 
any sort of PCL support.

Thanks

-- 
-- 
--
mono-cecil
--- 
You received this message because you are subscribed to the Google Groups 
"mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to