C# 如何让带有标志的枚举输出所有实值?
考虑下面的代码,如何让Console.WriteLine(Colors.All)输出“红、黄、蓝、绿”,而不是“All”C# 如何让带有标志的枚举输出所有实值?,c#,enums,C#,Enums,考虑下面的代码,如何让Console.WriteLine(Colors.All)输出“红、黄、蓝、绿”,而不是“All” 使用系统; 命名空间控制台应用程序1 { 班级计划 { 静态void Main(字符串[]参数) { Console.WriteLine(Colors.All);//All var noRed=Colors.All&~Colors.Red; Console.WriteLine(noRed);//黄色、蓝色、绿色 Console.ReadLine(); } } [旗帜] 枚举颜
使用系统;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(Colors.All);//All
var noRed=Colors.All&~Colors.Red;
Console.WriteLine(noRed);//黄色、蓝色、绿色
Console.ReadLine();
}
}
[旗帜]
枚举颜色
{
无=0,
Red=1要在您的案例中获得所需的结果,我认为最好的方法是对枚举使用description属性。查看我的代码,它将帮助您实现所需的功能
[Flags]
enum Colors
{
[Description("None")]
None=0,
[Description("Red")]
Red= 1<<0,
[Description("Burnt Yellow")]
Yellow = 1<<1,
[Description("Blue")]
Blue= 1<<2,
[Description("Green")]
Green=1<<3,
[Description("Red | Yellow| Blue | Green")]
All = 99
}
// Now create helper class and function to get enum description for associated value.
using System;
using System.ComponentModel;
using System.Reflection;
public static class EnumHelper
{
/// <summary>
/// Retrieve the description on the enum, e.g.
/// [Description("Bright Pink")]
/// BrightPink = 2,
/// Then when you pass in the enum, it will retrieve the description
/// </summary>
/// <param name="en">The Enumeration</param>
/// <returns>A string representing the friendly name</returns>
public static string GetDescription(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return en.ToString();
}
}
//Now use this function to get attribute for your enum value.
Console.WriteLine(EnumHelper.GetDescription(Colors.All));
[标志]
枚举颜色
{
[说明(“无”)]
无=0,
[说明(“红色”)]
Red=1要在您的案例中获得所需的结果,我认为最好的方法是对枚举使用description属性。查看我的代码,它将帮助您实现所需的功能
[Flags]
enum Colors
{
[Description("None")]
None=0,
[Description("Red")]
Red= 1<<0,
[Description("Burnt Yellow")]
Yellow = 1<<1,
[Description("Blue")]
Blue= 1<<2,
[Description("Green")]
Green=1<<3,
[Description("Red | Yellow| Blue | Green")]
All = 99
}
// Now create helper class and function to get enum description for associated value.
using System;
using System.ComponentModel;
using System.Reflection;
public static class EnumHelper
{
/// <summary>
/// Retrieve the description on the enum, e.g.
/// [Description("Bright Pink")]
/// BrightPink = 2,
/// Then when you pass in the enum, it will retrieve the description
/// </summary>
/// <param name="en">The Enumeration</param>
/// <returns>A string representing the friendly name</returns>
public static string GetDescription(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return en.ToString();
}
}
//Now use this function to get attribute for your enum value.
Console.WriteLine(EnumHelper.GetDescription(Colors.All));
[标志]
枚举颜色
{
[说明(“无”)]
无=0,
[说明(“红色”)]
Red=1enum
不允许重写它的ToString()
(就像在Java中一样),因此您必须为它编写一个帮助器类。我从String.Join(“,”值)开始
wherevalues
保存要打印的旧值。另请参见:如果内置的枚举到字符串行为不适合您,您需要自己实现一个替代方法。请参见标记的重复项以获取多个选项。在这种特殊情况下,如果您编写的变体仅返回在中设置的单个标志一个输入值,可以使用string.Join(“,”,…)
创建最后一个字符串。如果您提出的问题实际上与堆栈溢出中已有的任何问题都有明显不同,请在发布时附上一个好消息,说明您尝试了什么,并解释您做了哪些研究,发现了什么,以及为什么该研究没有找到解决方案。enum
没有不允许重写它的ToString()
(就像在Java中一样),因此必须为它编写一个帮助器类。我将从String.Join(“,”值)开始
wherevalues
保存要打印的旧值。另请参见:如果内置的枚举到字符串行为不适合您,您需要自己实现一个替代方法。请参见标记的重复项以获取多个选项。在这种特殊情况下,如果您编写的变体仅返回在中设置的单个标志一个输入值,可以使用string.Join(“,”,…)
创建最后一个字符串。如果您提出的问题实际上与堆栈溢出中已有的任何问题都有明显不同,请在发布时附上一个好消息,说明您尝试了什么,并解释您所做的研究、发现了什么,以及为什么该研究没有引导您找到解决方案。这可能有效,但很困难它可以工作,但很难重构。