繁体   English   中英

System.Reflection.Emit - 如何添加属性以返回类型定义?

[英]System.Reflection.Emit - How to add attribute to return type definition?

我正在通过 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);

缺少返回属性:(知道如何实现正确的行为吗?

提前致谢

这个有问题的代码运行良好,只是 C# 反汇编程序没有显示它,只有一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM