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. When you write
this in C#:
int? foo = 2;
The compiler spits out this:
call instance void [mscorlib]System.Nullable`1<int32>::.ctor(!0)
But when I write Cecil code like this:
MethodReference constructorReference = new MethodReference(".ctor",
typeWhichIsIndeedANullableTypeInstance, module.Import(typeof(void)),
true, false, MethodCallingConvention.Default);
constructorReference.Parameters.Add(new
ParameterDefinition(typeWhichIsIndeedANullableTypeInstance.GenericParameters[0]));
if (!module.MemberReferences.Contains(constructorReference))
{ module.MemberReferences.Add(constructorReference); }
// Emit the instruction: Call(constructorReference);
I get this CIL:
call instance void [mscorlib]System.Nullable`1<int32>::.ctor(int32)
This sucks, because peverify complains (justly) that no such token
exists. I have managed to work out I need to pass a GenericParameter
instance to the ParameterDefinition constructor which somehow references
the generic parameter to the Nullable`1 type. Something like this:
GenericParameter genericTokenTypeReference = new GenericParameter("T",
typeWhichIsIndeedANullableTypeInstance);
genericTokenTypeReference.Position = 0;
constructorReference.Parameters.Add(new
ParameterDefinition(genericTokenTypeReference));
But this actually generates this CIL:
call instance void [mscorlib]System.Nullable`1<int32>::.ctor(!-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.
Thanks in advance for any help at all, and do tell me if I'm making no
sense..
Max
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---