Hi Andrei,

I don't think it's a bug in Cecil, but admittedly, generics are
tricky. And the copious documentation doesn't help much ;)

// the Type that will be using the reference, Bar<TInBar>

TypeDefinition bar = module.GetType("Bar`1");

// get the definition of Nullable.Equals

AssemblyDefinition corelib = module.AssemblyResolver.Resolve("mscorlib");
TypeDefinition nullableDef = corelib.MainModule.Types.First(x =>
x.Name == "Nullable");
MethodDefinition equalsDef = nullableDefinition.Methods.First(x =>
x.Name == "Equals" && x.Parameters.Count == 2 && x.IsStatic);

// create a reference in module

MethodReference equalsInModule = module.Import(equalsDef);

// create the Equals<TInBar>(!!0, !!0) instance

GenericInstanceMethod equalsInstance = new
GenericInstanceMethod(equalsInModule);
equalsInstance.GenericArguments.Add(bar.GenericParameters[0]);

// then you can use equalsInstance in your module in your type Bar.

Let me know if this helps,

Jb

On Mon, Sep 8, 2014 at 3:23 PM, Andrei Korzenikov
<[email protected]> wrote:
> Hi,
>
> I have following class:
> public class Bar<T> where T: struct
> {
> }
>
> Then I try to import method
>  Nullable.Equals<T>(Nullable<T> n1, Nullable<T> n2) where T: struct
>
> I use following code:
> var bar = module.GetType ("Bar`1");
> var msCoreLibDefinition = module.AssemblyResolver.Resolve("mscorlib");
> var msCoreTypes = msCoreLibDefinition.MainModule.Types;
> TypeDefinition nullableDefinition = msCoreTypes.Where(x => x.Name ==
> "Nullable").First();
> var nullableEqualsMethod =
> module.Import(nullableDefinition).Resolve().Methods.Where(x => x.Name ==
> "Equals").First();
> var genericInstanceMethod = new GenericInstanceMethod(nullableEqualsMethod);
> genericInstanceMethod.GenericArguments.Add (bar.GenericParameters [0]);
> module.Import(genericInstanceMethod);
>
> Unfortunately, call of import method raises exception.
>
> Does anyone know wether it is a Mono.Cecil bug or I do something wrong?
>
> --
> --
> --
> 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.

-- 
-- 
--
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