所有枚举项转为字符串 (C#)

发布于 2024-07-16 07:09:54 字数 291 浏览 4 评论 0原文

如何将所有元素从枚举转换为字符串?

假设我有:

public enum LogicOperands {
        None,
        Or,
        And,
        Custom
}

我想要归档的内容类似于:

string LogicOperandsStr = LogicOperands.ToString();
// expected result:  "None,Or,And,Custom"

How to convert all elements from enum to string?

Assume I have:

public enum LogicOperands {
        None,
        Or,
        And,
        Custom
}

And what I want to archive is something like:

string LogicOperandsStr = LogicOperands.ToString();
// expected result:  "None,Or,And,Custom"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怀念你的温柔 2024-07-23 07:09:54
string s = string.Join(",",Enum.GetNames(typeof(LogicOperands)));
string s = string.Join(",",Enum.GetNames(typeof(LogicOperands)));
面犯桃花 2024-07-23 07:09:54

你必须做这样的事情:

var sbItems = new StringBuilder()
foreach (var item in Enum.GetNames(typeof(LogicOperands)))
{
    if(sbItems.Length>0)
        sbItems.Append(',');
    sbItems.Append(item);
}

或者在 Linq 中:

var list = Enum.GetNames(typeof(LogicOperands)).Aggregate((x,y) => x + "," + y);

You have to do something like this:

var sbItems = new StringBuilder()
foreach (var item in Enum.GetNames(typeof(LogicOperands)))
{
    if(sbItems.Length>0)
        sbItems.Append(',');
    sbItems.Append(item);
}

Or in Linq:

var list = Enum.GetNames(typeof(LogicOperands)).Aggregate((x,y) => x + "," + y);
葬花如无物 2024-07-23 07:09:54
string LogicOperandsStr 
     = Enum.GetNames(typeof(LogicOoperands)).Aggregate((current, next)=> 
                                                       current + "," + next);
string LogicOperandsStr 
     = Enum.GetNames(typeof(LogicOoperands)).Aggregate((current, next)=> 
                                                       current + "," + next);
忆依然 2024-07-23 07:09:54

虽然@Moose的答案是最好的,但我建议您缓存该值,因为您可能会经常使用它,但它在执行过程中100%不可能改变——除非您修改并重新编译枚举。 :)

就像这样:

public static class LogicOperandsHelper
{
  public static readonly string OperandList = 
    string.Join(",", Enum.GetNames(typeof(LogicOperands)));
}

Although @Moose's answer is the best, I suggest you cache the value, since you might be using it frequently, but it's 100% unlikely to change during execution -- unless you're modifying and re-compiling the enum. :)

Like so:

public static class LogicOperandsHelper
{
  public static readonly string OperandList = 
    string.Join(",", Enum.GetNames(typeof(LogicOperands)));
}
携君以终年 2024-07-23 07:09:54
foreach (string value in Enum.GetNames(typeof(LogicOoperands))
{
    str = str + "," + value;
}
foreach (string value in Enum.GetNames(typeof(LogicOoperands))
{
    str = str + "," + value;
}
时光沙漏 2024-07-23 07:09:54

将枚举转换为可以交互的内容的简单而通用的方法:

public static Dictionary<int, string> ToList<T>() where T : struct
{
   return ((IEnumerable<T>)Enum.GetValues(typeof(T))).ToDictionary(item => Convert.ToInt32(item), item => item.ToString());
}

然后:

var enums = EnumHelper.ToList<MyEnum>();

A simple and generic way to convert a enum to something you can interact:

public static Dictionary<int, string> ToList<T>() where T : struct
{
   return ((IEnumerable<T>)Enum.GetValues(typeof(T))).ToDictionary(item => Convert.ToInt32(item), item => item.ToString());
}

and then:

var enums = EnumHelper.ToList<MyEnum>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文