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

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

问题描述

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

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)