C#根据枚举值获取枚举
获取枚举值方法
public enum enum1{
测试1 = 123,
测试2 = 312,
测试3 = 431
}
//根据值获取枚举方法
public static Enum GetEnumByValue(Type enumType, string value)
{
return Enum.Parse(enumType, value) as Enum;
}
#调用方法
//调用方法
Enum aa = GetEnumByValue(typeof(enum1), "123");
Console.WriteLine(aa);
输出值:
使用情景
public class M_RackUpDown
{
public int id { get; set; }
public string goodsid { get; set; }
public int orgid { get; set; }
//此参数为平台id
public string platform { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string skuid { get; set; }
public int temp { get; set; }
public string OriginalPrice { get; set; }
//这里获取平台名称
//OrderSourceType这个枚举是平台id枚举
public string platformName { get => GetEnumByValue(typeof(OrderSourceType), platform.ToString()).ToString(); }
private static Enum GetEnumByValue(Type enumType, string value)
{
return Enum.Parse(enumType, value) as Enum;
}
}
public class OrderSourceType{
美团外卖 = 1001,
}