简体   繁体   English

如何在c#中将字符和int值传输到枚举中?

[英]How to transfer character and int value into enum in c#?

I have defined a Enum as blow, it includes int type and character type.我已经将 Enum 定义为 Blow,它包括 int 类型和字符类型。

I tried to parse string into enum, but it would occur error when input character.我试图将字符串解析为枚举,但输入字符时会发生错误。

How to fix this?如何解决这个问题?

//parse successfully.
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), "1"); 

//error:Requested value 'A' was not found.
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), "A"); 

 public enum MyEnum
    {
        [Description("Des1")]
        Status1 = 1,
        [Description("Des2")]
        Status2 = 2,
        [Description("Des3")]
        Status3 = 3,
        [Description("Des4")]
        Status4 = 4,
        [Description("Des7")]
        Status5 = 5,
        [Description("Des6")]
        Status6 = 6,
        [Description("Des7")]
        Status7 = 7,
        [Description("Des8")]
        Status8 = 8,
        [Description("Des9")]
        Status9 = 9,
        [Description("DesA")]
        StatusA = 'A',
        [Description("DesB")]
        StatusB = 'B',
        [Description("DesC")]
        StatusC = 'C',
    }

Update:更新:

OK, I think I need check the input is digit or not to do different process.好的,我想我需要检查输入是否为数字以进行不同的处理。 Code as Below did work, thanks for everyone's advise.下面的代码确实有效,谢谢大家的建议。

    string inputParam = "A";
    int useLess;
    MyEnum myEnum;
    var isDigit = int.TryParse(inputParam, out useLess);
    if (isDigit) {
        myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), inputParam);
    }
    else
    {
        myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), ((int)Convert.ToChar(inputParam)).ToString());
    }

The issue here is that the enum is in fact based on int values, so the char values are being converted to int .这里的问题是枚举实际上基于int值,因此 char 值正在转换为int

In other words, this:换句话说,这:

StatusA = 'A',

Is actually converted to this:实际上转换成这样:

StatusA = 65,

Because the actual numeric value of StatusA is 65 , you'd have to do:因为StatusA的实际数值是65 ,所以你必须这样做:

MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), "65");

or alternatively, to make it more obvious that 65 is 'A' :或者,更明显的是65'A'

MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), ((int)'A').ToString());

Note that enum.Parse() will parse either the name of the enum member, or a string containing the numeric value of the enum member, so either "65" or "StatusA" would work.请注意, enum.Parse()将解析枚举成员的名称或包含枚举成员数值的字符串,因此“65”或“StatusA”都可以使用。

So all of the following lines will return the same result:因此,以下所有行都将返回相同的结果:

MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), "65");
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), ((int)'A').ToString());
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), "StatusA");

Enum.Parse only works for the names or the numeric values of the associated constant values. Enum.Parse仅适用于关联常量值的名称数值 of the enum members.枚举成员。 'A' is neither of those. 'A'两者都不是。 If you have passed the string "65" to the method, it would have worked.如果您已将字符串"65"传递给该方法,它将起作用。

Note that how you write the associated constant value doesn't matter - whether it's 'A' , or 65 , or 0b1000001 or 0x41 , they all look the same to the compiler.请注意,如何编写相关的常量值并不重要——无论是'A'650b1000001还是0x41 ,它们对编译器来说都是一样的。 Enum.Parse only accepts the second way of writing it. Enum.Parse只接受第二种编写方式。

To convert the character 'A' to the enum type MyEnum , you just need a cast:要将字符'A'转换为枚举类型MyEnum ,您只需要一个MyEnum转换:

MyEnum foo = (MyEnum)'A';

However, since you have a string "A" here, you need to get the first character (assuming that you are sure that the string represents one of the associated constant values):但是,由于这里有一个字符串"A" ,您需要获取第一个字符(假设您确定该字符串代表一个关联的常量值):

MyEnum foo = (MyEnum)"A"[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM