private static void PushTargetMethodInfo(ILGenerator IL, MethodBuilder generatedMethod, MethodInfo method)
{
if (method.IsGenericMethodDefinition)
{
// We want the generated code to load a MethodInfo instantiated with the
// current generic type parameters. I.e.:
// MethodInfo methodInfo = methodof(TheClass.TheMethod<T>(...)
//
// We need to instantiate the open generic method definition with the type
// arguments from the generated method. Using the open method definition
// directly works on .Net 2.0, which might be FW bug, but fails on 4.0.
//
// Equivalent pseudo-code:
// MethodInfo mi = methodof(TheClass.TheMethod<>)
// versus
// MethodInfo mi = methodof(TheClass.TheMethod<T0>)
var instantiatedMethod = method.MakeGenericMethod(generatedMethod.GetGenericArguments());
IL.Emit(OpCodes.Ldtoken, instantiatedMethod);
}
else
{
IL.Emit(OpCodes.Ldtoken, method);
}
System.Type declaringType = method.DeclaringType;
if (declaringType.IsGenericType)
{
IL.Emit(OpCodes.Ldtoken, declaringType);
IL.Emit(OpCodes.Call, getGenericMethodFromHandle);
}
else
{
IL.Emit(OpCodes.Call, getMethodFromHandle);
}
IL.Emit(OpCodes.Castclass, typeof(MethodInfo));
}