我有一行一行创建一个数组的代码:
vm.CardChoice = new[] {
new ParamViewModel { Id = 0, Name = CC.JLPT5.Text() },
new ParamViewModel { Id = 1, Name = CC.JLPT4.Text() },
new ParamViewModel { Id = 2, Name = CC.JLPT3.Text() },
new ParamViewModel { Id = 3, Name = CC.JLPT2.Text() },
new ParamViewModel { Id = 4, Name = CC.JLPT1.Text() },
new ParamViewModel { Id = 5, Name = CC.JFBP1.Text() },
new ParamViewModel { Id = 5, Name = CC.JFBP2.Text() },
new ParamViewModel { Id = 7, Name = CC.JFBP3.Text() },
new ParamViewModel { Id = 8, Name = CC.All.Text() },
new ParamViewModel { Id = 9, Name = CC.Catg.Text() },
new ParamViewModel { Id = 10, Name = CC.Cat.Text() },
new ParamViewModel { Id = 11, Name = CC.C1.Text() },
new ParamViewModel { Id = 12, Name = CC.C2.Text() },
new ParamViewModel { Id = 13, Name = CC.C3.Text() },
};
是否有任何方法可以基于ENUM本身进行操作,或者是逐行编码的唯一方法?这是我拥有的ENUM:
using System;
namespace Japanese.Enums
{
public enum CC
{
JLPT5 = 0,
JLPT4 = 1,
JLPT3 = 2,
JLPT2 = 3,
JLPT1 = 4,
JFBP1 = 5,
JFBP2 = 6,
JFBP3 = 7,
All = 8,
Catg = 9,
Cat = 10,
C1 = 11,
C2 = 12,
C3 = 13
}
public static partial class Extensions
{
public static string Text(this CC cardChoice)
{
switch (cardChoice)
{
case CC.JLPT5: return "N5";
case CC.JLPT4: return "N4";
case CC.JLPT3: return "N3";
case CC.JLPT2: return "N2";
case CC.JLPT1: return "N1";
case CC.JFBP1: return "JFBP1";
case CC.JFBP2: return "JFBP2";
case CC.JFBP3: return "JFBP3";
case CC.All: return "ALL";
case CC.Catg: return "GROUP";
case CC.Cat: return "> GROUP";
case CC.C1: return "C1";
case CC.C2: return "C2";
case CC.C3: return "C4";
}
return "";
}
public static string LongText(this CC cardChoice)
{
switch (cardChoice)
{
case CC.JLPT5: return "Japanese Language Proficiency Test Level N5";
case CC.JLPT4: return "Japanese Language Proficiency Test Level N4";
case CC.JLPT3: return "Japanese Language Proficiency Test Level N3";
case CC.JLPT2: return "Japanese Language Proficiency Test Level N2";
case CC.JLPT1: return "Japanese Language Proficiency Test Level N1";
case CC.JFBP1: return "Japanese for Busy People 1";
case CC.JFBP2: return "Japanese for Busy People 2";
case CC.JFBP3: return "Japanese for Busy People 3";
case CC.All: return "All Available Words";
case CC.Catg: return "High Level Group";
case CC.Cat: return "Low Level Group";
case CC.C1: return "Custom 1";
case CC.C2: return "Custom 2";
case CC.C3: return "Custom 3";
}
return "";
}
}
}
答案 0 :(得分:3)
Enum.GetValues()
是您需要的:
Enum.GetValues(typeof(CC)).Cast<CC>().Select(x => new ParamViewModel{ Id = (int)x, Name = x.Text()});
答案 1 :(得分:1)
检出System.Enum
类。它具有许多有用的方法,例如GetNames
或GetValues
,使您可以读取enum
类型的所有条目。与某些LINQ配对,半自动处理它们真的很简单。
一旦有了一个枚举条目列表,就可以将它们简单地转换为int
来获取数值,或者可以使用Enum.GetName
或.ToString
来获取它们的名称。
您可能还希望查看本文,了解一些非LINQ提示,您可以使用System.Enum.xxx方法进行操作。
https://blogs.msdn.microsoft.com/haibo_luo/2005/08/28/get-names-and-values-of-enum-members/
但是,这只会帮助您提取直接描述enum
类型的数据。这意味着CC.JLPT3->(int)2或CC.JLPT3->(string)“ JLPT3”。就是这样,因为enum
的定义仅包含此类数据,enum
中没有其他内容可以读取。.
没有可以自动翻译的开箱即用的魔术
CC.JLPT3 is a string "N3"
或者那个
CC.JLPT3 is a string "Japanese Language Proficiency Test Level N3"
这些是自定义的演示数据,与枚举本身完全无关。
要获得某种简化操作的魔术,可能最好的方法是将其放入字典中,以便于读取/使用/更新。或枚举中的属性,以方便重用和简化编码:< / p>
public enum CC
{
[ShortText("N5")]
[LongText("Japanese Language Proficiency Test Level N5")]
JLPT5 = 0,
[ShortText("N4")]
[LongText("Japanese Language Proficiency Test Level N4")]
JLPT4 = 1,
[ShortText("N3")]
[LongText("Japanese Language Proficiency Test Level N3")]
JLPT3 = 2,
...
但是,看起来很酷,但没有开箱即用的东西,您必须:
我不确定这是否值得,但可以肯定,这是很好的锻炼方法。