GetFunctionPointer 호출 시 System.InvalidProgramException 예외 발생
현상은 간단합니다. 제네릭 메서드인 경우 그에 대해 GetFunctionPointer를 호출하면,
using System;
using System.Reflection;
using System.Threading;
public class Program
{
public static void GenericMethod<T>(T obj)
{
Thread.Sleep(1);
}
static unsafe void Main()
{
Type type = typeof(Program);
MethodInfo mi = type.GetMethod("GenericMethod", BindingFlags.Static | BindingFlags.Public);
{
string fullName = string.Format("{0}.{1}", type.FullName, mi.Name);
IntPtr methodBody = mi.MethodHandle.GetFunctionPointer(); // 예외 발생
Console.WriteLine(fullName);
}
}
}
MethodInfo.MethodHandle.GetFunctionPointer 메서드 호출에서 다음과 같은 예외가 발생합니다.
An unhandled exception of type 'System.InvalidProgramException' occurred in mscorlib.dll
Additional information: Common Language Runtime detected an invalid program.
이게... 이상한 듯 하면서도 사실 당연한 겁니다. 왜냐하면, 제네릭인 경우 컴파일러가 생성한 IL 단계에는 제네릭의 타입이 정해지지 않은 상태이고, 실제 메서드가 사용될 때 기계어 컴파일이 타입에 따라 확장되면서 컴파일되기 때문에 GenericMethod 자체의 FunctionPointer 값을 대표할 수 없는 것입니다. 가령 그 값이 0x00100으로 반환되었다고 해도 GenericMethod<int>(int obj)로 확장된 메서드의 FunctionPointer는 또 다른 값이 될 수 있는 것입니다.
그래서, 원래는 제네릭 메서드의 제대로 된 FunctionPointer를 구하고 싶다면 다음과 같이 해줘야 합니다.
MethodInfo mi = type.GetMethod("GenericMethod", BindingFlags.Static | BindingFlags.Public);
MethodInfo intMethod = mi.MakeGenericMethod(typeof(int));
string fullName = string.Format("{0}.{1}", type.FullName, intMethod.Name);
IntPtr methodBody = intMethod.MethodHandle.GetFunctionPointer();
Console.WriteLine(fullName); // Program.GenericMethod
그런데, 재미있는 것은 클래스 수준의 제네릭 인자가 있는 것은 또 잘됩니다.
public class GenericClass<T>
{
public static void Test(T arg)
{
Console.WriteLine(arg);
}
}
type = typeof(GenericClass<>);
mi = type.GetMethod("Test", BindingFlags.Static | BindingFlags.Public);
{
string fullName = string.Format("{0}.{1}", type.FullName, mi.Name);
IntPtr methodBody = mi.MethodHandle.GetFunctionPointer();
Console.WriteLine(fullName); // GenericClass`1.Test
}
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]