471,327 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,327 software developers and data experts.

Reflection & Generics

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.

Jan 4 '06 #1
4 2332
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

Jan 4 '06 #2
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.

Jan 4 '06 #3
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

Jan 4 '06 #4
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.

Jan 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Anders Borum | last post: by
2 posts views Thread by Marc | last post: by
1 post views Thread by Rathish P S | last post: by
1 post views Thread by uttara | last post: by
2 posts views Thread by Wiktor Zychla [C# MVP] | last post: by
reply views Thread by Konrad Kaczanowski | last post: by
18 posts views Thread by riftimes | last post: by
7 posts views Thread by =?Utf-8?B?TXJOb2JvZHk=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.