Hey Max,
On 6/21/07, Max Bolingbroke <[EMAIL PROTECTED]> wrote:
> Hi, it's me again. You must be getting pretty bored of this by now, but
> I'm having even more trouble with generics and Cecil.
No problem, I'm perfectly aware that the Generic side of Cecil for
that is way too complicated, but I'm still wondering on how to get a
clean API for that.
> MethodReference constructorReference = new MethodReference(".ctor",
> typeWhichIsIndeedANullableTypeInstance, module.Import(typeof(void)),
> true, false, MethodCallingConvention.Default);
> constructorReference.Parameters.Add(new
> ParameterDefinition(typeWhichIsIndeedANullableTypeInstance.GenericParameters[0]));
The fact that GenericParameters [0] contains Int32 here is clearly a bug.
typeWhichIsIndeedANullableTypeInstance here should be a
GenericInstanceType, with an ElementType being a TypeReference: the
actual Nullable`1<T>.
This one is the one you want to use as a IGenericParameterProvider you
want to get the GenericParameter from.
> if (!module.MemberReferences.Contains(constructorReference))
> { module.MemberReferences.Add(constructorReference); }
Again, this is something that sort of sucks, this won't work if there
is another instance of a MethodReference with the same signature,
you'll end up having twice the reference, because I don't override
Equals anywhere. But this requires some black magic to work perfectly.
> GenericParameter genericTokenTypeReference = new GenericParameter("T",
> typeWhichIsIndeedANullableTypeInstance);
> genericTokenTypeReference.Position = 0;
>
> constructorReference.Parameters.Add(new
> ParameterDefinition(genericTokenTypeReference));
>
> call instance void [mscorlib]System.Nullable`1<int32>::.ctor(!-1)
That's because I don't actually use the Position property when writing
the assembly, but rather the position of the generic parameter in its
context, hence the -1.
> Close but no cigar. I assume the "-1" means some error has occured deep
> within the bowels of Cecil because my arguments are screwy. I really
> don't like the fact that I'm constructing the GenericParameter manually,
> and I feel sure there must be some way to retrieve such an object from
> Cecil.. but I don't have a clue how.
The issue here is that you want to be <<generic>> (not in the Cecil
sense of generic).
If not, you could write something like:
int? a = 2;
ConstructorInfo c = a.GetConstructor (...);
MethodReference meth = module.Import (c);
> Thanks in advance for any help at all, and do tell me if I'm making no
> sense..
Well, thanks for pointing out this lack of Cecil.
Still, I'm wondering on the API to expose to ease the work you want to achieve.
--
Jb
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---