Reflection.Emit:获取具有GenericTypeParameterBuilder类型参数的构造类型的MethodInfo

时间:2016-11-07 22:31:59

标签: c# generics reflection.emit

我尝试Reflection.Emit一个泛型类,它实现了使用类的通用参数构造的泛型接口,例如这样:

class Foo<U>: IEquatable<U>
{
    bool IEquatable<U>.Equals(U other) { /* ... */ }
}

我很难为MethodInfo方法获得IEquatable<U>.Equals(U)

反映构造的类型IEquatable<U>会引发NotSupportedException因为UGenericTypeParameterBuilder,而TypeBuilder.GetMethod(IEquatable<U>, IEquatable<T>.Equals(T))会返回一个奇怪的IEquatable<U>.Equals(T)方法。< / p>

任何帮助表示赞赏!

测试代码:

// define class Foo<U>
var tb = moduleBuilder.DefineType("Foo");
var genParams = tb.DefineGenericParameters("U");

// IEquatable<T>.Equals(T) method
var miEqualsT = typeof(IEquatable<>).GetMethod("Equals", typeof(IEquatable<>).GetGenericArguments());

// IEquatable<U> constructed type
var iEquatableU = typeof(IEquatable<>).MakeGenericType(genParams);

// now trying to get IEquatable<U>.Equals(U) method
MethodInfo miEqualsU;
try { miEqualsU = iEquatableU.GetMethod("Equals", genParams); }
catch (NotSupportedException) { Console.WriteLine("Reflecting constructed interface not supported."); }

miEqualsU = TypeBuilder.GetMethod(iEquatableU, miEqualsT);
var declaringType =$"{miEqualsU.DeclaringType.Name}<{miEqualsU.DeclaringType.GenericTypeArguments[0].Name}>";
var parameterType = miEqualsU.GetParameters()[0].ParameterType;
Console.WriteLine($"TypeBuilder.GetMethod() returns {declaringType}.{miEqualsU.Name}({parameterType.Name})");

// OUTPUT:
// Reflecting constructed interface not supported.
// TypeBuilder.GetMethod() returns IEquatable`1<U>.Equals(T)

1 个答案:

答案 0 :(得分:3)

您已经使用miEqualsT获得了正确的方法。以下是您使用该方法实现界面的方法:

var assemblyName = new AssemblyName { Name = "asd" };
var moduleBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run).DefineDynamicModule(assemblyName.Name);

var tb = moduleBuilder.DefineType("Foo");
var genParams = tb.DefineGenericParameters("U");
var miEqualsT = typeof(IEquatable<>).GetMethod("Equals", typeof(IEquatable<>).GetGenericArguments());

var myMethod = tb.DefineMethod("Equals", MethodAttributes.Public | MethodAttributes.Virtual, typeof(bool), genParams);

var il = myMethod.GetILGenerator();
il.Emit(OpCodes.Ldstr, "I was invoked!");
il.Emit(OpCodes.Call, GetMethod<string>(a => Console.WriteLine(a)));
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Ret);

tb.AddInterfaceImplementation(typeof(IEquatable<>).MakeGener‌​icType(genParams));
tb.DefineMethodOverride(myMethod, miEqualsT);

var t = tb.CreateType();

//Hack for demonstration
private MethodInfo GetMethod<T>(Expression<Action<T>> thing)
{
    return ((thing as LambdaExpression).Body as MethodCallExpression).Method;
}

然后使用它:

var genericType = t.MakeGenericType(typeof(int));
var tObj = Activator.CreateInstance(genericType);

var method = genericType.GetMethods()
    .Where(m => m.Name == "Equals" && m.DeclaringType == genericType)
    .First();

method.Invoke(tObj, new object[] { 5 });

或者:

var tObj = Activator.CreateInstance(genericType);
DoIt(tObj as IEquatable<int>);

private void DoIt(IEquatable<int> obj)
{
    obj.Equals(5);
}