我通过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.