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

2023-09-06 22:10:59 作者:4.狂的像阵风

我通过System.Reflection.Emit定义某些类型。想象一下,我想有方法的签名与一些自定义属性,是这样的:

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);

我用这样的code生成它:

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?

在此先感谢

推荐答案

这code的问题是运作良好就在C#中的反汇编并没有显示它,IL人做。

This code in question is working well just the C# disassembler does not shows it, il one does.