作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何从枚举转换为字符串,反之亦然?
假设枚举可以包含多个标志,我如何才能获得一个枚举包含的所有标志的列表?
最佳答案
有一个库可以实现枚举所需的大部分功能,而且性能更好 https://github.com/TylerBrinkley/Enums.NET
从枚举到字符串: [Enum Instance].ToString();
示例:使用 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) 应该忽略字符大小写?]);
示例:使用 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()}");
结果:是否成功? : True, 结果 : Alt, Control, Shift
或
// 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
,
分隔符“无,Alt,Control,Shift”
= “无,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
关于c# - 如何在(字符串、标志列表、带标志的枚举)之间转换枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38410350/
我是一名优秀的程序员,十分优秀!