[mono-cecil] return type of closed generic method

157 views
Skip to first unread message

Jan

unread,
May 1, 2010, 11:18:54 AM5/1/10
to mono-cecil
Hi,

I habe a MethodReference to DateTime?.get_Value() and I need the
return type of this method. With Cecil 0.9 the return type is 'T' and
not 'DateTime'

method.Fullname returns '!0
System.Nullable`1<System.DateTime>::get_Value()'

Is there a way to get the real return type? Do I have to replace the
generic parameter by myself?

Jan

--
--
mono-cecil

Jb Evain

unread,
May 1, 2010, 11:41:16 AM5/1/10
to mono-...@googlegroups.com
Hey,
Yep. Cecil, in that case, unless reflection, won't resolve them for
you. You have to resolve the parameters to arguments from the
declaring type and the declaring method.

--
Jb Evain <j...@nurv.fr>

--
--
mono-cecil

Jan

unread,
May 1, 2010, 12:38:36 PM5/1/10
to mono-cecil
Hi JB,

How can I get the tye? I have inspected the MethodDefinition in the
debugger and cannot see a correlation between !0 and the DateTime.

Thanks

Jan

--
--
mono-cecil

Jb Evain

unread,
May 1, 2010, 12:58:20 PM5/1/10
to mono-...@googlegroups.com
Hey,

On Sat, May 1, 2010 at 6:38 PM, Jan <jan.ble...@gmail.com> wrote:
> How can I get the tye? I have inspected the MethodDefinition in the
> debugger and cannot see a correlation between !0 and the DateTime.

If you have:

!0 System.Nullable`1<System.DateTime>::get_Value()

That's a MethodReference, whose DeclaringType is a:

GenericInstanceType
ElementType:
TypeReference
Name: Nullable`1
GenericParameters: !0
GenericArguments: System.DateTime

And the ReturnType of the MethodReference is the instance of the
GenericParameter on the Nullable`1 TypeReference.
So you have to map the parameter !0 to its corresponding argument, in
that case, System.DateTime.

Jan

unread,
May 1, 2010, 5:25:48 PM5/1/10
to mono-cecil
Got it working, here is my code:

public static TypeReference GetReturnType(this MethodReference
self)
{
TypeReference returnType = self.ReturnType;
if (returnType.IsGenericParameter)
{
GenericParameter tmp = (GenericParameter)returnType;
GenericInstanceType owner = tmp.Owner as
GenericInstanceType;
if (owner != null)
returnType = owner.GenericArguments[tmp.Position];
else
returnType =
((GenericInstanceMethod)self).GenericArguments[tmp.Position];
}
return returnType;
}

Jan

--
--
mono-cecil
Reply all
Reply to author
Forward
0 new messages