gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:08:02 32 4
gpt4 key购买 nike

我正在尝试 Reflection.Emit一个泛型类,它实现了一个用类的泛型参数构造的泛型接口(interface),例如这个:

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

我正在努力获得 MethodInfo对于 IEquatable<U>.Equals(U)方法。

反射(reflect)构造类型IEquatable<U>抛出 NotSupportedException因为UGenericTypeParameterBuilder ,而 TypeBuilder.GetMethod(IEquatable<U>, IEquatable<T>.Equals(T))返回一个奇怪的 IEquatable<U>.Equals(T)方法。

感谢任何帮助!

测试代码:

// 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)

最佳答案

您已经使用 miEqualsT 获得了正确的方法。以下是使用方法实现接口(interface)的方式:

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);
}

关于c# - Reflection.Emit:为具有 GenericTypeParameterBuilder 类型参数的构造类型获取 MethodInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40475877/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com