TypeDefiniton with all its generic type information lost. In other
On May 8, 12:11 pm, Gábor Kozár <
kozarga...@gmail.com> wrote:
> Hi,
>
> I've had the same issue before, but I've managed to solve it with the help
> of Jb.
>
> What you need to do is the following:
>
> TypeReference genericArg = _moduleDefinition.TypeSystem.Int32;
> GenericInstanceType genericType =
> _moduleDefinition.Import(typeof(Nullable<>))
> .MakeGenericInstanceType(genericArg);
> MethodDefinition ctor = genericType.Resolve().Methods.First(m =>
> m.IsConstructor && m.Parameters.Count == 1);
>
> Note that if you try to emit a newobj instruction with 'ctor' with its
> operand at this point, you'll get invalid IL, because when you use
> Resolve(), you lose all generic argument information. We need to provide
> that manually.
> For that, you have to add your generic argument to ctor.DeclaringType, or
> alternatively, you can use Rocks' MakeGenericInstanceType, like so:
>
> ctor.DeclaringType = ctor.DeclaringType.MakeGenericInstanceType(genericArg);
>
> Now you can emit your newobj instruction with 'ctor' being its argument.
> Note that you _might_ need to clone ctor.DeclaringType first (I'm not sure,
> I don't have my code in front of me atm), but you get the general idea.
>
> Hope this helps!
>