[英]System.Reflection.Emit - How to add attribute to return type definition?
I'm defining some types via System.Reflection.Emit.我正在通过 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提前致谢
This code in question is working well just the C# disassembler does not shows it, il one does.这个有问题的代码运行良好,只是 C# 反汇编程序没有显示它,只有一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.