Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reflection & Generics

1 view
Skip to first unread message

Dov Tendler

unread,
Jan 4, 2006, 2:46:55 AM1/4/06
to
Hi All,
I would like to get MethodInfo of current instantiated generic method
call.

Consider the following function call:
C.f<int>(5);

Where f is defined as follows:
class C
{
static public void f<G>(G g)
{
// Try to get the MethodInfo of f() with the current generic
parameters indtantiation
}
}

I have tried the following code inside f():

// ThisMethodInfo represents the unbounded method,
// e.g., info.ToString() returns "Void f[G](G g)"
MethodInfo info =
(MethodInfo)MethodInfo.GetCurrentMethod();

// Using the stackTrace gives the same unbounded info
System.Diagnostics.StackTrace stack = new
System.Diagnostics.StackTrace();
MethodInfo info2 =
(MethodInfo)stack.GetFrames()[0].GetMethod();

// To get the instantiated generic method call I can use
MakeGenericMethod().
// However, I'm looking for general code, which is not
based on the specific
// method signature (e.g., G may be as a return value, or
as a generic parameter for
// one of the function argument...)
MethodInfo info3 = info.MakeGenericMethod(new Type[] {
g.GetType() });
// info3.GetString() returns "Void f[Int32](Int32)"

// I also tried to work with RuntimeMethodHandle and
// MethodInfo.GetMethodFromHandle()
// but I always get the handle for the unbounded method.

Any help will be appreciated!
Thanks in advanced,
Dubi.

Jon Skeet [C# MVP]

unread,
Jan 4, 2006, 3:44:05 AM1/4/06
to
Dov Tendler wrote:
> I would like to get MethodInfo of current instantiated generic method
> call.

<snip>

You can use typeof(T) within a generic method:

using System;
using System.Reflection;

public class Test
{
static T ShowMethod<T>()
{
MethodInfo info = (MethodInfo)MethodBase.GetCurrentMethod();
Console.WriteLine(info.MakeGenericMethod(typeof(T)));
return default(T);
}


static void Main()
{
int i = ShowMethod<int>();
}
}

Jon

Dov Tendler

unread,
Jan 4, 2006, 4:05:57 AM1/4/06
to
Hi Jon,
I need to write a general code for any method, without knowing what the
generic parameters are. In other wrods, I would like to use the
MethodInfo without using T explicitly.
Using info.GetGenericParameters() returns the generic types of the
method, but I don't know how to convert it to the real instantiated
type.
Many Thanks for your help,
Dubi.

Jon Skeet [C# MVP]

unread,
Jan 4, 2006, 4:39:13 AM1/4/06
to
Dov Tendler wrote:
> I need to write a general code for any method, without knowing what the
> generic parameters are. In other wrods, I would like to use the
> MethodInfo without using T explicitly.
> Using info.GetGenericParameters() returns the generic types of the
> method, but I don't know how to convert it to the real instantiated
> type.

I *suspect* you can't do this.

Out of interest, what's the overall goal here?

Jon

Dov Tendler

unread,
Jan 4, 2006, 7:09:34 AM1/4/06
to
Hi Jon,
Your last example gives me the hint I'm looking for.
Since I use instrumentation techinques, I may use the ldtoken MSIL
instruction on each method generic parameter to generate the relevant
RuntimeMethodHandle of the instantiated type.
0 new messages