System.Reflection.Emit - 如何添加属性以返回类型定义?
我通过 System.Reflection.Emit 定义一些类型。想象一下,我想要带有一些自定义属性的方法签名,如下所示:
[return: MyAttr]
MyType MethodName([MyOtherAttr] MyOtherType);
我使用这样的代码来生成它:
TypeBuilder t = assembly.DefineType(...);
MethodAttributes methodAttr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot;
MethodBuilder method = t.DefineMethod("MethodName", methodAttr, typeof(MyType), new Type[] { typeof(MyOtherType) });
method.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
// return type
ParameterBuilder pbr = method.DefineParameter(0, ParameterAttributes.Retval, null);
CustomAttributeBuilder cabr = GetMyAttrBuilder();
pbr.SetCustomAttribute(cabr);
// parameter
ParameterBuilder pbp = method.DefineParameter(1, ParameterAttributes.None, null);
CustomAttributeBuilder cabp = GetMyOtherAttrBuilder();
pbp.SetCustomAttribute(cabp);
t.CreateType();
但是生成的方法签名是:
MyType MethodName([MyOtherAttr] MyOtherType);
缺少返回属性:(知道如何实现正确的行为吗?
提前致谢
I'm defining some types via System.Reflection.Emit. Imagine that I want to have method signature with some custom attributes, something like this:
[return: MyAttr]
MyType MethodName([MyOtherAttr] MyOtherType);
I use such code to generate it:
TypeBuilder t = assembly.DefineType(...);
MethodAttributes methodAttr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot;
MethodBuilder method = t.DefineMethod("MethodName", methodAttr, typeof(MyType), new Type[] { typeof(MyOtherType) });
method.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
// return type
ParameterBuilder pbr = method.DefineParameter(0, ParameterAttributes.Retval, null);
CustomAttributeBuilder cabr = GetMyAttrBuilder();
pbr.SetCustomAttribute(cabr);
// parameter
ParameterBuilder pbp = method.DefineParameter(1, ParameterAttributes.None, null);
CustomAttributeBuilder cabp = GetMyOtherAttrBuilder();
pbp.SetCustomAttribute(cabp);
t.CreateType();
But the generated method signature is:
MyType MethodName([MyOtherAttr] MyOtherType);
The return attribute is missing :( Any idea how to achieve right behavior?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码运行良好,只是 C# 反汇编程序没有显示它,而有人却显示了它。
This code in question is working well just the C# disassembler does not shows it, il one does.