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

3

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

问题描述

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

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.

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14

函数委托与函数
Func Delegate vs Function(函数委托与函数)...
2023-11-11 C#/.NET开发问题
6

具有未知类型的 CreateDelegate
CreateDelegate with unknown types(具有未知类型的 CreateDelegate)...
2023-11-11 C#/.NET开发问题
5