Reflection & Generics

D

Dov Tendler

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.
 
J

Jon Skeet [C# MVP]

Dov said:
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
 
D

Dov Tendler

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.
 
J

Jon Skeet [C# MVP]

Dov said:
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
 
D

Dov Tendler

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.
Many Thanks for your help,
Dubi.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top