简体   繁体   中英

How to convert a type to enum

I'm trying to get an enum from a type , I'll explain, I have this enums (I used the String Enumerations) which are explained here .

public enum ENUM1
{
    [StringValue("CodeIP597")] Code = 1,
    [StringValue("InfoYes")] Info = 3
}

public enum ENUM2
{
    [StringValue("CodeNoIP")] Code = 1,
    [StringValue("Info95p")] Info = 3
}

the method "General" is this:

private static void General(Type enumType)
{
   var Enu = enumType.toEnum();  //i want something like this
   Console.WriteLine(StringEnum.GetStringValue(Enu.Code));           
}

I have a lot of equals enums each of these always contains the attribute "Code" and "Info" , so i want that for every enumType that i put as a parameter, in the variable console return me the String Value.

For example : if i call this method

General(typeof(ENUM1));

the console will be : CodeIP597

if i call this method

General(typeof(ENUM2));

the console will be : CodeNoIP

How could i do? Thanks to all in advance

Assuming your attribute looks something like this:

public class StringValue : Attribute
{
    public string Name { get; private set; }
    public StringValue(string name)
    {
        this.Name = name;
    }
}

Then you'll have to use reflection to pull out the attribute and its value since there is no relation between your enums besides their value names:

private static void General(Type enumType)
{
    if (!enumType.IsEnum)
        throw new ArgumentException("Not an enum type");

    var values = enumType.GetFields();

    var code = values.First(f => f.Name == "Code");
    var info = values.First(f => f.Name == "Info");

    string codeString = code.GetCustomAttributes(false).OfType<StringValue>().First().Name;
    string infoString = info.GetCustomAttributes(false).OfType<StringValue>().First().Name;

    Console.WriteLine(codeString);
    Console.WriteLine(infoString);
}

It's not exactly the same as what you have in terms of output, but demonstrates how to get your string values. You may also need to add additional checks that they're the types you expect or have the attributes you expect.

您最好使用一种称为enum.GetValues()的方法。

If you look at GetStringValue() in the code project link you shared, the Enum value is converted to a string before it is processed. You could rewrite GetStringValue() to accept a Type and a string rather than a typed enum value:

public static string GetStringValue(Type enumType, string valueStr)
{
    //Look for our 'StringValueAttribute' 
    //in the field's custom attributes
    FieldInfo fi = enumType.GetField(valueStr);
    StringValueAttribute[] attrs = 
       fi.GetCustomAttributes(typeof (StringValueAttribute), 
                               false) as StringValueAttribute[];
    if (attrs.Length > 0)
    {
        return attrs[0].Value;
    }

    return null;
}

I removed the cache since that's not included inside the function but that should be an easy task to reintroduce.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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