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.
<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
I *suspect* you can't do this.
Out of interest, what's the overall goal here?
Jon