使用具有属性的自定义属性的枚举进行“反向查找”。 [英] 'Reverse lookup' with Enums that have custom attributes that have properties.

查看:82
本文介绍了使用具有属性的自定义属性的枚举进行“反向查找”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我获得了一个包含许多枚举的项目,这些枚举使用编码系统的自定义属性。大多数属性都是字符串,因为它们用在要在系统之间传递的XML文件中。它们也将在UI中使用。



我遇到的问题是2折
1.这些枚举及其结构是指定的由外部来源

2.我需要在给定另一个属性属性时查找它。



例如

我用较少的属性改变以使其更简单

I have been given a project that contains numerous enums which use custom attributes for coding systems. The properties for the most part are strings because they are used in XML files which are to be passed around between systems. They are also to be used in the UI.

The problem I have is 2 fold
1. these enums and their structure are specified by an external source
2. I need to lookup one attribute property when given another.

e.g.
I've changed with less properties to make it simpler

[[CodedValueAttribute(Code = "2344", Name = "Ford XD")]
FordXF,
CodedValueAttribute(Code = "2345", Name = "Mazda 121")]
Mazda121,





进来的代码是例如2344,我必须最终在用户界面中使用福特XF。

最好的方法是使其尽可能通用,其中枚举类型和自定义属性类型可以不同?



提前感谢您。



The code the comes in is for example 2344 and I have to end up with "Ford XF" in the UI.
What is the best approach for this to get it as generic as possible, where enum type and the custom attribute type can be different?

Thanking you in advance.

推荐答案

您可以在我的两篇CodeProject文章中找到非常相似的代码:

枚举类型不枚举!解决.NET和语言限制 [ ^ ],

人类可读的枚举元数据 [ ^ ]。



请阅读它们,它们是我在3部分循环中的两个枚举,第二个和第三个循环在以前的。这些属性在第二个中使用,用于为枚举成员生成人类可读的名称,但是也可以阅读第一个属性,其中解释了有关枚举的所有基本原理。如果您感到困惑,请询问您的后续问题。



-SA
You can find very similar code in two of my CodeProject articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^],
Human-readable Enumeration Meta-data[^].

Please read them, they are the two from my 3-part cycle on enumerations, second and third ones based in the previous ones. The attributes are used in the second one, for generation of human-readable names for enumeration member, but it's good to read the first one, too, where all the fundamentals about enumerations are explained. If you fill confused, please ask your follow-up questions.

—SA


这篇关于使用具有属性的自定义属性的枚举进行“反向查找”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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