在. NET 8下运行.
IJoinable<T1, T2>
已经作为通用接口存在于代码库中. 它声明了一个方法,T2 Join(T1 value)
. 用于类型参数(lType
、rType
、joinedType
)的类型是在运行时动态生成的TypeBuilder变量.
var joinType = typeof(IJoinable<,>).MakeGenericType(rType, joinedType);
lType.AddInterfaceImplementation(joinType);
var joinMethod = lType.DefineMethod("Join", MethodAttributes.Public | MethodAttributes.Virtual, joinedType, [rType]);
var gen = joinMethod.GetILGenerator();
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldarg_1);
gen.Emit(OpCodes.Call, ctor);
gen.Emit(OpCodes.Ret);
lType.DefineMethodOverride(joinMethod, joinType.GetMethod("Join")!);
当我在最后一行调用joinType.GetMethod()
时,它抛出了NotSupportedException
,因为不知何故,它最终在System.Reflection.Emit.TypeBuilderInstantiation.GetMethodImpl()
内部,而System.Reflection.Emit.TypeBuilderInstantiation.GetMethodImpl()
的整个方法体就是抛出NotSupportedException
.
如果这不起作用,那该怎么办?