System.Reflection.Emit - 如何添加属性返回类型定义? [英] System.Reflection.Emit - How to add attribute to return type definition?

查看:369
本文介绍了System.Reflection.Emit - 如何添加属性返回类型定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于System.Reflection.Emit - 如何添加属性返回类型定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆