我想要使用一些文件,我需要知道将每个数据放在哪里。一列称为“类别”。
我可以有很多类别,我想构建一个枚举来支持它们。
public enum GasKeyWords
{
Gas,
Fuel,
Gas&Fuel
}
public enum EducationKeyWord
{
Education,
Books&Supplies,
StudentLoan
Tuition
}
我在想这样的事情
foreach(var r in records)
{
Categories.GasKeyWords GasKeyWords;
bool valid = Enum.TryParse<Categories.GasKeyWords>(r, out GasKeyWords);
if (valid)
{
// add to group
}
}
并继续使用TryParse,直到我找到一个它也可以解析的组。我不确定这是否是最佳方式(我总是乐于接受更好的想法)。
然而,我现在遇到的问题是我不能使用“&amp;”在枚举名称中。我无法控制这些类别名称,所以我无法改变它。
答案 0 :(得分:4)
为什么不使用Description标记来解决此问题。虽然它并不完全是它的设计目标,但是如果你对此有疑问,那么你总是可以发明自己的属性类。
这是我为获取对象的描述属性而编写的一个快速脏方法
public static string GetDescription(this object enumeration)
{
//Again, not going to do your condition/error checking here.
//Normally I would have made this extension method for the Enum type
but you won't know the type at compile time when building the list of descriptions but ..
// you will know its an object so that is how I cheated around it.
var desc = enumeration.GetType().GetMember(enumeration.ToString())[0]
.GetCustomAttributes(typeof(DescriptionAttribute), true) as DescriptionAttribute[];
if (desc != null && desc.Length > 0)
{
return desc[0].Description;
}
return enumeration.ToString();
}
这是一个快速的实用工具方法,用于获取对象的所有描述符列表作为列表
public static IList<string> GetDescriptions<E>(E type)
{
//I'm not going to do your error checking for you.
//Figure out what you want to do in the event that this isn't an enumeration.
//Or design it to support different cases. =P
var list = new List<string>();
foreach(var name in Enum.GetNames(typeof(E)))
{
var enumObj = (E) Enum.Parse(typeof(E), name);
list.Add(enumObj.GetDescription());
}
return list;
}
现在将这些用于你需要的效果
public enum GasKeyWords
{
[Description("Gas")]
Gas,
[Description("Fuel")]
Fuel,
[Description("Gas&Fuel")]
GasFuel
}
...
var gasKeywords = Util.GetDescriptions<GasKeywords>();
foreach(var r in records)
{
var found = gasKeywords.Contains(r);
if (found)
{
}
}
我不是一个泛型大师。如果有人对如何解决GetDescriptions()方法的输入问题有一些想法,那将是受欢迎的。不幸的是,我无法设置一个期望Enum
的类型约束,这会使代码变得不那么友好。
答案 1 :(得分:1)
我在自己的项目中做过类似的事情......为了简洁而编辑......
用法:GetType(Operators).GetTitle(tName))
实现:
<AttributeUsage(AttributeTargets.Field, AllowMultiple:=False, Inherited:=False)>
Public Class EnumItemAttribute
Inherits Attribute
Public Property Title As String = String.Empty
Public Property Description As String = String.Empty
End Class
<Extension()>
Public Function GetTitle(ByVal tEnum As Type, ByVal tItemName As String) As String
Dim tFieldInfo As FieldInfo = Nothing
tFieldInfo = tEnum.GetField(tItemName)
For Each tAttribute As EnumItemAttribute In tFieldInfo.GetCustomAttributes(GetType(EnumItemAttribute), False)
Return tAttribute.Title
Next
Return String.Empty
End Function
答案 2 :(得分:0)
C#标识符必须以下划线开头,Unicode类Lu,Ll,Lt,Lm,Lo或Nl中的字符,或其中一个字符的转义符。 所有其他字符必须来自Unicode类Lu,Ll,Lt,Lm,Lo,Nl,Mn,Mc,Nd,Pc或Cf,或其中之一的转义。
标识符可以以@开头,但这不是标识符的一部分,但用于允许与关键字相同的单词作为标识符。因此,myVar
和@myVar
是相同的标识符,而@if
是有效的标识符,而我们无法使用if
,因为它会与关键字冲突。
(C#规范第2.4.2节)。
&
属于Po类,因此不在规则范围内。
对于符合CLS的标识符,ECMA-335要求标识符遵循http://www.unicode.org/reports/tr15/tr15-18.html附件7,标准化为NFC,并且没有单独的案例名称,这更加严格。
简而言之,你不能。
更重要的是,你为什么要这样做?如何从Gas&Fuel
Gas&Fuel
Gas
表达Fuel
&
作为{{1}}运算符操作数的表达式?