Jb Evain wrote:
> Hey,
>
> Your mail is as confused as what's your trying to accomplish is
> confusing as well as this sentence is.
>
:-)
I thought I explained it pretty clearly, but obviously not! I'll try
again. Imagine you have some assembly which contains a method like:
void Foo(int? bar)
{
// Some stuff
}
Now, I want to run Cecil over this assembly, and when we come to that
method in it we insert at the start of the method body code that e.g.
checks if "bar.HasValue" is true and throws an exception if it isn't:
void Foo(int? bar)
{
if (!bar.HasValue)
{ throw new ArgumentNullException("bar", "Yes this is a stupid
example"); }
// Some stuff, again
}
So, what I was describing below was a function that needs to know the
MethodReference for the HasValue property getter. Once it has this it
can emit code like:
ldarga bar
call [mscorlib]System.Nullable`1<int>.get_HasValue
brtrue LABEL
ldstr "bar"
ldstr "Yes this is a stupid example"
newobj [mscorlib]System.ArgumentNullException
throw
LABEL: nop
// Some stuff goes here
Thats fine if all I want to do is use Nullable<int>: I can use
this.module.Import(typeof(Nullable<int>).GetProperty("HasValue").GetGetMethod()).
But actually I want to deal with any Nullable parameter type, and there
is no such syntax as
this.module.Import(typeof(Nullable<?>).GetProperty("HasValue").GetGetMethod())
(it would not make much sense). So I must try and get the
MethodReference for my specific Nullable type by hand:
MethodReference("get_HasValue", parameter.ParameterType,
module.Import(typeof(bool)), true, false, MethodCallingConvention.Default);
But the resulting IL does not verify if you try and "call" using this
MethodReference, and the problem seems to be the MetadataToken, like I was
going on about below.
Can I accomplish this using Cecil?
Cheers,
Max
>
> On 6/11/07, Max Bolingbroke <[EMAIL PROTECTED]> wrote:
>
>> I'm trying to access the HasValue property of the generic type
>> Nullable<T>, but after spending hours thrashing around with the code I
>> just can't get it to work. The code below is the culprit, it takes a
>> nullable ParameterDefinition parameter and tries to invoke the HasValue
>> property of that parameter:
>>
>> // This works, but only if the parameter is Nullable<int>
>> MethodReference hasValueReference =
>> this.module.Import(typeof(Nullable<int>).GetProperty("HasValue").GetGetMethod());
>>
>> Console.WriteLine(hasValueReference.MetadataToken);
>>
>> // I WANT this to work, it should be fine for any Nullable<*>. But the
>> metadata token is invalid (says PEVerify) and I don't have a clue what
>> it is or what should go in it :-)
>> // It defaults to Module [0x0000], but the Imported MethodReference
>> above has MemberRef [0x001b].. but this only works for Nullable<int>,
>> you can't just set it into the MethodReference below and expect it to work..
>> MethodReference oldHasValueReference = new
>> MethodReference("get_HasValue", parameter.ParameterType,
>> module.Import(typeof(bool)), true, false, MethodCallingConvention.Default);
>>
>> Console.WriteLine(oldHasValueReference.MetadataToken);
>>
>> // Then emit some code:
>> //Ldarga(parameter);
>> //Call(oldHasValueReference);
>>
>> Thanks for any help you can give me on this point..
>> Max
>>
>
>
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---