属性的顺序是否有保证? [英] Does the order of attributes have any guarantees?

查看:92
本文介绍了属性的顺序是否有保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将多个属性应用于成员,例如

  [Foo] 
[Bar]
void Baz(){...}

然后是CLR / .NET提供的任何保证。关于通过反射进行检索的顺序的说明(例如 Attribute.GetCustomAttributes )?这些方法的文档并未对其进行明确说明,虽然看起来确实是按应用顺序返回它们的情况,但我不想依靠未记录的行为,因为这只是在麻烦。 p>

关于为什么这很重要的原因,请想象一下该属性用于指示该方法应具有一系列应按特定顺序运行的拦截器。

解决方案

否来自 17.2属性规范似乎并非如此。


在这样的列表中指定属性
的顺序,以及
顺序,其中排列在同一程序实体上的
所附加的节,
不重要。例如,
属性规范[A] [B],
[B] [A],[A,B]和[B,A]相当于



If multiple attributes are applied to a member, e.g.

[Foo]
[Bar]
void Baz() { ... }

Then are any guarantees made by the CLR/.NET specifications as to what order they will be retrieved in when retrieved by reflection (e.g. Attribute.GetCustomAttributes)? The documentation for these methods does not make it explicit, and while it does seem to be the case that they are returned in the order they are applied, I don't want to rely on undocumented behaviour as that's just asking for trouble.

As for why this might be important, imagine the attributes are used to indicate that the method should have a series of interceptors, which should run in a specific order.

解决方案

No from 17.2 Attribute specification it does not seems so.

The order in which attributes are specified in such a list, and the order in which sections attached to the same program entity are arranged, is not significant. For instance, the attribute specifications [A][B], [B][A], [A, B], and [B, A] are equivalent.

这篇关于属性的顺序是否有保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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