属性的顺序有什么保证吗?

编程入门 行业动态 更新时间:2024-10-26 12:29:08
本文介绍了属性的顺序有什么保证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

属性的顺序有什么保证吗?

本文发布于:2023-11-06 07:58:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563126.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   顺序   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!