具有泛型类型参数的 DynamicMethod
是否可以使用泛型类型参数定义 DynamicMethod? MethodBuilder 类具有 DefineGenericParameters 方法。 DynamicMethod 有对应的吗? 例如,是否可以使用 DynamicMethod 创建具有类似于给定打击的签名的方法?
void T Foo<T>(T a1, int a2)
Is it possible to define a DynamicMethod with generic type parameters? The MethodBuilder class has the DefineGenericParameters method. Does the DynamicMethod have a counterpart? For example is it possible to create method with a signature like the one given blow using DynamicMethod?
void T Foo<T>(T a1, int a2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎不可能:正如您所见,
DynamicMethod
没有DefineGenericParameters
方法,并且它从其继承了
基类,仅抛出MakeGenericMethod
MethodInfoNotSupportedException
。几种可能性:
This doesn't appear to be possible: as you've seen
DynamicMethod
has noDefineGenericParameters
method, and it inheritsMakeGenericMethod
from itsMethodInfo
base class, which just throwsNotSupportedException
.A couple of possibilities:
AppDomain.DefineDynamicAssembly
DynamicMethod
once for each set of type arguments实际上有一种方法,它并不完全通用,但你会明白的:
你可以这样称呼它:
Actually there is a way, it's not exactly generic but you'll get the idea:
You can call it like this: