可以使用泛型类型参数定义DynamicMethod吗?MethodBuilder类具有DefineGenericParameters方法。DynamicMethod有对应的产品吗?例如,是否可以使用DynamicMethod创建具有类似blow的签名的方法?
void T Foo<T>(T a1, int a2)发布于 2014-03-26 12:35:17
实际上有一种方法,它不是完全通用的,但你会明白的:
public delegate T Foo<T>(T a1, int a2);
public class Dynamic<T>
{
public static readonly Foo<T> Foo = GenerateFoo<T>();
private static Foo<V> GenerateFoo<V>()
{
Type[] args = { typeof(V), typeof(int)};
DynamicMethod method =
new DynamicMethod("FooDynamic", typeof(V), args);
// emit it
return (Foo<V>)method.CreateDelegate(typeof(Foo<V>));
}
}你可以这样称呼它:
Dynamic<double>.Foo(1.0, 3);发布于 2009-04-25 11:59:21
这似乎是不可能的:正如您所看到的,DynamicMethod没有DefineGenericParameters方法,并且它从其MethodInfo基类继承了MakeGenericMethod,而后者只是抛出了NotSupportedException。
有几种可能性:
AppDomain.DefineDynamicAssemblyhttps://stackoverflow.com/questions/788618
复制相似问题