是否GetCustomAttributes()preserve在.NET中的属性顺序? [英] Does GetCustomAttributes() preserve the attribute order in .NET?

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

问题描述

标题pretty的多少说明了一切。当我做一些反思过我的课,将MemberInfo.GetCustomAttributes()方法preserve属性的顺序的成员,还是不?官方的文档不出声的一种方式或其他。

The title pretty much says it all. When I'm doing some reflection through my classes, will the MemberInfo.GetCustomAttributes() method preserve the order of attributes on a member, or not? The official documentation does not say anything one way or the other.


如果你想知道为什么我需要这个,下面是完整的解释。这是漫长的,而不是需要的问题,因为它是现在提出的,但也许有人能想出的替代解决方案,以更大的问题不涉及依赖属性枚举顺序。

In case you're wondering why I would need this, here's the full explanation. It's lengthy and not needed for the question as it is posed now, but perhaps someone can come up with an alternative solution to the greater problem which does not involve relying on attribute enumeration order.

我想做一个灵活的框架为(ASP.NET)申请,预计将有pretty的长寿命。过当然,这将获得许多形式,将必须从菜单中访问。为了让生活更容易为开发商来我做了一个 MenuItemAttribute ,你可以申请一个窗体类。此属性在其构造无限量的字符串参数,它允许开发人员指定在什么地方会的形式驻留在菜单中。典型的使用例子是像 [菜单项(本公司,客户,订单)] 这将那么意味着菜单中应该有一个项目公司下会有一个项目客户下会有一个项目订单 - 它会再开的形式。单个形式可以有几个这些属性如果需要的话 - 它随后将来自若干位置中菜单acessible

I'm trying to make a flexible framework for an (ASP.NET) application that is expected to have a pretty long lifetime. Over the course it will gain many forms that will have to be accessible from the menu. To make life easier for the developers to come I've made a MenuItemAttribute which you can apply to a form's class. This attribute has an unlimited amount of string parameters in its constructor which allows a developer to specify where exactly will the form reside in the menu. A typical usage example would be something like [MenuItem("Company", "Clients", "Orders")] which would then mean that the menu should have an item "Company" under which there would be an item "Clients" under which there would be an item "Orders" - which would then open the form. A single form can have several of these attributes if needed - it will then be acessible from several places in the menu.

显然整个菜单是通过所有的课程在我的组件枚举和搜索此属性内置的运行时间。不过最近我已经得到了菜单项应该在predefined的方式进行排序的请求。具有相关功能的形式应该是相邻的菜单。请注意,这不是字母排序,而是$​​ P $ pdefined为了使开发人员指定的。

Obviously the whole menu is built in runtime by enumerating through all the classes in my assemblies and searching for this attribute. However recently I've gotten a request that the menu items should be sorted in a predefined way. Forms that have related functionality should be next to each other in the menu. Note that this is NOT alphabetical sorting, but a predefined order that the developers specify.

这则带来的问题 - 我该如何指定这些属性的顺序?由于一个 MenuItemAttribute 描述了整个层次,秩序规范还应包括层级的整个订单号(或至少是一部分)。对于层次结构的只是下层的顺序号是不充分的。

This then brings the problem - how do I specify the order in these attributes? Since one MenuItemAttribute describes a whole hierarchy, the order specification should also include order numbers for the whole (or at least a part) of the hierarchy. An order number for just the lower level of the hierarchy is not sufficient.

我可以做另一个属性 - MenuItemOrderHintAttribute ,但是这将随后带来的问题的案件时有多个 MenuItemAttribute 。因此,原来的问题。

I could make another attribute - MenuItemOrderHintAttribute, but that would then bring problems with cases when there is more than one MenuItemAttribute. Hence the original question.

我还可以延长 MenuItemAttribute 采取任何两个数组或对数组的形式,但随后将复杂的语法很多。最后的想法是,我可以使字符串具有特殊的格式,但是这将是相当混乱恕我直言。

I could also extend the MenuItemAttribute to take either two arrays or an array of pairs, but that then would complicate the syntax a lot. The last idea is that I could make the strings have special format, but that would be rather messy IMHO.


好了,我已经得到了另一种思路。让我们用排序的乔恩斯基特建议。这将允许以指定层次结构的最后一层,而不是高的人的顺序。但我可以修改属性,使其不仅适用于类,但也给大会本身。在这种情况下,菜单项不会有关联的形式。在组件级别这些属性可随后被用来指定中较高层次的顺序

OK, I've got another idea. Let's use the ordering that Jon Skeet suggested. This will allow to specify the order for the last level of the hierarchy, not the higher ones. But I could modify the attribute so that it applies not only to classes, but also to the assembly itself. In this case the menu item would not have an associated form. At the assembly level these attributes could then be used to specify the ordering among higher levels of the hierarchy.

这是再集中与分散的菜单系统之间的权衡。任何思考为什么这是一个坏主意?

This is then a tradeoff between a centralized and decentralized menu system. Any thoughts why this would be a bad idea?

推荐答案

我把一个额外的(可选)值在MenuItemAttribute构造,这是订单或优先:

I'd put a single extra (optional) value in the MenuItemAttribute constructor, which is "order" or "priority":

[MenuItem(0, "Company", "Clients", "Orders")]
[MenuItem(1, "Foo", "Bar", "Baz")]

我并不是说这将是pretty的,但它会有效地允许您指定的顺序。

I'm not saying it would be pretty, but it would effectively allow you to specify the ordering.

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

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