如何从枚举转换为字符串,反之亦然?
考虑到枚举可以包含多个标志的情况,如何获得枚举包含的所有标志的列表?
答案 0 :(得分:13)
有一个库可以实现从枚举中获得的大部分功能,并具有更好的性能 https://github.com/TylerBrinkley/Enums.NET
从枚举到字符串: [Enum Instance].ToString();
示例:using System.Windows.Input;
ModifierKeys mk = ModifierKeys.None | ModifierKeys.Alt | ModifierKeys.Shift | ModifierKeys.Control;
mk.Tostring();
返回:
Alt, Control, Shift
注意:“无”标志已被删除
从字符串到枚举:
Enum.TryParse([(string) value],out [store variable]);
或
Enum.Parse(typeof([Enum Type]), [(string) value], [(bool) should ignore character case ?]);
示例:using System.Windows.Input;
// Way 1:
ModifierKeys outenum;
bool successful = Enum.TryParse("None,Alt,Control,Shift", out outenum);
Console.WriteLine($"Is Successful ? : {successful}, result : {outenum.ToString()}");
结果:Is Successful ? : True, result : Alt, Control, Shift
OR
// Way 2:
ModifierKeys outenum = (ModifierKeys)Enum.Parse(typeof(ModifierKeys), "None,Alt,Control,Shift", true);
Console.WriteLine(outenum.ToString());
结果:Alt, Control, Shift
从枚举标志到列表
示例:
ModifierKeys mk = ModifierKeys.None | ModifierKeys.Alt | ModifierKeys.Shift | ModifierKeys.Control;
List<ModifierKeys> mklist =
mk
.ToString() // Convert the enum to string
.Split(new[] { "," } , StringSplitOptions.RemoveEmptyEntries) // Converts the string to Enumerable of string
.Select(//converts each element of the list to an enum, and makes an Enumerable out of the newly-converted items
strenum =>
{
ModifierKeys outenum;
Enum.TryParse(strenum , out outenum);
return outenum;
})
.ToList(); // Creates a List from the Enumerable
结果:{ModifierKeys.Alt , ModifierKeys.Shift , ModifierKeys.Control}
从枚举标记列表到枚举
示例:
ModifierKeys[] mk = {ModifierKeys.None, ModifierKeys.Alt, ModifierKeys.Control, ModifierKeys.Shift};
ModifierKeys mkc = mk.Aggregate((prev, next) => prev | next);
Console.WriteLine(mkc.ToString());
结果:Alt, Control, Shift
从字符串列表到枚举
示例:
string[] mk = {"None", "Alt", "Control", "Shift"};
ModifierKeys mkc = mk.Select(x => {ModifierKeys outenum; Enum.TryParse(x, out outenum); return outenum;}).Aggregate((prev , next) => prev | next);
Console.WriteLine(mkc.ToString());
结果:Alt, Control, Shift
,
分隔符"None , Alt,Control, Shift"
= "None,Alt,Control,Shift"
我写了一个简单的Generic EnumConverter类,可以节省你的时间
public class EnumConverter<T> where T : struct, IConvertible, IFormattable
{
private Type EnumType { get; set; }
public Type UnderlyingType
{
get
{
return Enum.GetUnderlyingType(EnumType);
}
}
public EnumConverter()
{
if (typeof(T).IsEnum)
EnumType = typeof(T);
else
throw new ArgumentException("Provided type must be an enum");
}
public IEnumerable<T> ToFlagsList(T FromSingleEnum)
{
return FromSingleEnum.ToString()
.Split(new[] { "," } , StringSplitOptions.RemoveEmptyEntries)
.Select(
strenum =>
{
T outenum = default(T);
Enum.TryParse(strenum , true , out outenum);
return outenum;
});
}
public IEnumerable<T> ToFlagsList(IEnumerable<string> FromStringEnumList)
{
return FromStringEnumList
.Select(
strenum =>
{
T outenum = default(T);
Enum.TryParse(strenum , true , out outenum);
return outenum;
});
}
public T ToEnum(string FromString)
{
T outenum = default(T);
Enum.TryParse(FromString , true , out outenum);
return outenum;
}
public T ToEnum(IEnumerable<T> FromListOfEnums)
{
var provider = new NumberFormatInfo();
var intlist = FromListOfEnums.Select(x => x.ToInt32(provider));
var aggregatedint = intlist.Aggregate((prev , next) => prev | next);
return (T)Enum.ToObject(EnumType , aggregatedint);
}
public T ToEnum(IEnumerable<string> FromListOfString)
{
var enumlist = FromListOfString.Select(x =>
{
T outenum = default(T);
Enum.TryParse(x , true , out outenum);
return outenum;
});
return ToEnum(enumlist);
}
public string ToString(T FromEnum)
{
return FromEnum.ToString();
}
public string ToString(IEnumerable<T> FromFlagsList)
{
return ToString(ToEnum(FromFlagsList));
}
public object ToUnderlyingType(T FromeEnum)
{
return Convert.ChangeType(FromeEnum , UnderlyingType);
}
}
用法:
EnumConverter<ModifierKeys> conv = new EnumConverter<ModifierKeys>();
//conv.ToEnum
//conv.ToFlagsList
//conv.ToString