如果多个属性应用于一个成员,例如
If multiple attributes are applied to a member, e.g.
[Foo]
[Bar]
void Baz() { ... }
那么 CLR/.NET 规范是否保证了它们在通过反射检索时将按什么顺序检索(例如 Attribute.GetCustomAttributes
)?这些方法的文档没有明确说明,虽然它们似乎是按照它们的应用顺序返回的,但我不想依赖未记录的行为,因为那只是自找麻烦.
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 属性规范 好像不是这样.
No from 17.2 Attribute specification it does not seems so.
属性的顺序在这样的列表中指定,并且附加部分的顺序安排相同的节目实体,不重要.例如,属性规范 [A][B],[B][A]、[A,B] 和 [B,A] 是等价的.
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.
这篇关于属性的顺序有什么保证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!