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