简体   繁体   English

将字符串数组转换为枚举

[英]Convert string array to enum on the fly

I am binding an enum to a property grid like this: 我将enum绑定到属性网格,如下所示:

public enum myEnum
{
    Ethernet,
    Wireless,
    Bluetooth
}

public class MyClass
{
    public MyClass()
    {
        MyProperty = MyEnum.Wireless;
    }

    [DefaultValue(MyEnum.Wireless)]
    public MyEnum MyProperty { get; set; }
}

public Form1()
{
    InitializeComponent();
    PropertyGrid pg = new PropertyGrid();
    pg.SelectedObject = new MyClass();
    pg.Dock = DockStyle.Fill;
    this.Controls.Add(pg);
}

My problem: I get data on the fly when the program is running. 我的问题:我在程序运行时动态获取数据。 I read the network adapter then store adapter names to myArray like this: 我读了网络适配器,然后将适配器名称存储到myArray如下所示:

string[] myArray = new string[] { };
myArray[0] = "Ethernet";
myArray[1] = "Wireless";
myArray[2] = "Bluetooth";

Is possible convert myArray to myEnum on the fly using c#? 可以使用c# myEnum地将myArray转换为myEnum吗? Thank You. 谢谢。

Sure! 当然! This is all you need: 这就是你所需要的:

IEnumerable<myEnum> items = myArray.Select(a => (myEnum)Enum.Parse(typeof(myEnum), a));

You'll want to use Enum.Parse : http://msdn.microsoft.com/en-us/library/essfb559.aspx 您将要使用Enum.Parsehttp//msdn.microsoft.com/en-us/library/essfb559.aspx

MyProperty = (myEnum)Enum.Parse(typeof(myEnum), myArray[0]);

How you'll want to use that with your array I guess is up to your needs. 您希望如何在阵列中使用它我认为可以满足您的需求。

EDIT: By any chance, is it feasible to store your adapter names to your array as enumerations in the first place? 编辑:在任何可能的情况下,首先将您的适配器名称作为枚举存储到您的阵列是否可行? Is there some reason the array must be strings? 是否有某些原因数组必须是字符串?

If your source data is not something entirely reliable, you may want to consider converting only the items that can actually be parsed, using TryParse() and IsDefined() . 如果您的源数据不是完全可靠的,您可能需要考虑使用TryParse()IsDefined()仅转换实际可以解析的项目。

Getting an array of myEnums from an array of strings can be performed by the following code: 从字符串数组中获取myEnums数组可以通过以下代码执行:

myEnum [] myEnums = myArray
    .Where(c => Enum.IsDefined(typeof(myEnum), c))
    .Select(c => (myEnum)Enum.Parse(typeof(myEnum), c))
    .ToArray();

Note that IsDefined() only works with a single enumerated value. 请注意, IsDefined()仅适用于单个枚举值。 If you have a [Flags] enum, combinations fail the test. 如果您有[Flags]枚举,组合将无法通过测试。

You do not have to use Parse if you want to get the name of the enum's value. 如果要获取枚举值的名称,则不必使用Parse。 Don't use .ToString() , use this instead. 不要使用.ToString() ,而是使用它。 For example if I want to return Ethernet I would do the following: 例如,如果我想返回Ethernet我会执行以下操作:

public enum myEnum
{
    Ethernet,
    Wireless,
    Bluetooth
}

In your main class add this line of code: 在您的主类中添加以下代码行:

var enumName = Enum.GetName(typeof(myEnum), 0); //Results = "Ethernet"

If you want to enumerate over the Enum Values you could do this to get the values: 如果要枚举Enum值,可以执行此操作以获取值:

foreach (myEnum enumVals in Enum.GetValues(typeof(myEnum)))
{
    Console.WriteLine(enumVals);//if you want to check the output for example
}

在循环中为数组中的每个元素使用Enum.Parse

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

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