将字典转换为枚举 [英] convert dictionary to enum

查看:276
本文介绍了将字典转换为枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一本字典< string,int>取自数据库表
我如何将此字典转换为静态枚举-ProgressStatus.ProgressCode枚举
(ProgressStatus是类名,ProgressCode应该是枚举名).您不能在应用程序中轻松使用的一种,因为枚举名称通常在编译时是固定的(实际上已经替换为等效的数字).
例如,如果在运行时创建枚举,则不能在代码中将其用作if语句或switch语句的一部分,因为在编译时会在枚举存在之前对其进行检查.

您想做什么,您认为这会有所帮助?


字典和枚举类型之间没有对应关系,因此原则上您不能转换"它.字典中的数据存储在其实例成员中,而不是以类型存储(即,该数据不是静态的).因此,要从字典中获取数据,您需要一个实例.相反,枚举是一种类型,数据存储在其静态成员中.

Dictionary<string, int>的每个给定实例可能与某个给定的枚举类型匹配(键可能是枚举成员的名称,值是其整数值);因此您只能进行检查.通常,转换"是指根据某个实例计算某个实例.在这种情况下,这是不可能的,因为枚举类型的实例仅对应一个值,但是字典表示一组键-值对.从这个意义上讲,这个问题没有任何意义(双关语意料之外的:-)).

也许您需要完全不同的东西:枚举类型的发出.在这种情况下,尚不清楚如何使用它:在编译过程中未声明它.尽管如此,这是发出它的方法:使用System.Reflection.Emit.EnumBuilder,请参见 http://msdn.microsoft.com/en-us/library/system.reflection.emit.enumbuilder.aspx [枚举类型不枚举!解决.NET和语言限制 [ ^ ].

不过,您的活动目的对我而言仍然是个谜.我不确定您的想法首先是否有意义.这就是为什么您总是应该从解释您的最终目标开始您的问题.

-SA


 ProgressCode结果;
Enum.TryParse< ProgressCode>("  out 结果); 


Hi,

I have a dictionary<string,int> that is taken from DB table
How can i convert this dictionary to a static enum - ProgressStatus.ProgressCode enum
(ProgressStatus is the class name and ProgressCode should be the enum name)

解决方案

You probably can, using Reflection, but it''s a very, very nasty job, and one that you can''t easily use in your application, since enum names are generally fixed (and indeed replaced with the numeric equivalent) at compile time.
For example, if you create the enum at run time, you cannot use it in your code as part of an if statement, or switch statement, since those are checked at compile time, before the enum exists.

What are you trying to do, that you think this will help?


There is not correspondence between dictionary and enumeration type, so you cannot "convert" it in principle. The data in dictionary is stored in its instance members, not in the type (that is, this data is not static). So, to get data from the dictionary, you need one of the instances. In contrast, enumeration is a type, and data is stored in its static members.

Each given instance of Dictionary<string, int> may or may not match some given enumeration type (key is the name of the enumeration member, value is its integer value); so you only can check it up. Normally, "convert" means calculating of some instance based on some instance. This is not possible in this case, as the instance of enumeration type corresponds to just one value, but the dictionary represents a set of key-value pairs. In this sense, the question makes no sense (pun unintended :-)).

Maybe you need something completely different: emitting of the enumeration type. In this case, it''s not clear how can you use it: it''s not declared during compilation. Nevertheless, here is how to emit it: use System.Reflection.Emit.EnumBuilder, see http://msdn.microsoft.com/en-us/library/system.reflection.emit.enumbuilder.aspx[^].

For some fine detail on how to work with enumerations, please see my article: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

Still, the purpose of your activity remains mysterious to me. I''m not sure your idea makes sense in first place. That''s why you always should start your question with explanation of your ultimate goals.

—SA


ProgressCode result;
Enum.TryParse<ProgressCode>("value from database", out result);


这篇关于将字典转换为枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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