如何访问枚举值 [英] how to access enum values

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

问题描述



i面临访问枚举值的问题。



i制作枚举



enum caroptions

{

maruti alto,

swift lx,

xylo xuv

};

但是我在intellisense中的价值即将到来

for maruti alto只有maruti

并且swift lx只是很快。



所有值在空间未来之后。



i am access比如按钮点击事件



caroptions option = option。(枚举中的值名称)



please帮助我。

Hi,
i am facing a problem to access enum values.

i made an enum

enum caroptions
{
maruti alto,
swift lx,
xylo xuv
};
but while i am acessing values in intellisense is coming
for maruti alto is only maruti
and swift lx is only swift.

for all values after space is not coming.

i am accessing like on button click event

caroptions option = option.(name of values inside enum)

please help me.

推荐答案

我相信你现在已经意识到,Enum标签不能使用空格,但是,有一种方法可以将描述性文本与每个空格相关联枚举元素:
As I am sure you realize by now, Enum labels cannot use white-space, but, there is a way you can associate descriptive text with each Enum element:
// requires: using System.ComponentModel;

enum caroptions
{
    [Description("maruti alto")]
    maruti,
    [Description("swift lx")]
    swift,
    [Description("xylo xuv")]
    xylo
}

但是,在运行时访问该描述性文本需要使用System.Reflection;你真的想要付出这样的代价:

However, accessing that descriptive text at run-time requires using System.Reflection; do you really want to be paying the price of using code like this:

FieldInfo fi = caroptions.maruti.GetType().GetField(caroptions.maruti.ToString());

string description = ((DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false))[0].Description;

是的,当然你可以把这个代码打包在一个方法中,或者把它变成一个扩展方法,并减少它的冗长,但是你仍然在为一个小的回报做一些繁重的工作。

Yes, of course you could package that code up in a method, or make it into an extension method, and reduce its verbosity, but you are still doing some heavy-lifting, imho, for a small pay-off.


嗨试试这个





hi try this


enum caroptions
 {
 maruti_alto,
 swift_lx,
 xylo_xuv
 };

caroptions option = option.maruti_alto;





MSDN [ ^ ]


不要在你的枚举中留出空间



参考这个: - [ ^ ]
don't give space in your enum

refer this:- [^]


这篇关于如何访问枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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