[英]“&” in an enum?
我要消耗一些文件,並且我需要知道每個數據的存放位置。 一欄稱為“類別”。
我可以有很多類別,並且我想建立一個枚舉來支持它們。
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,直到找到可以解析的組為止。 我不確定這是否是最好的方法(我一直對更好的想法持開放態度)。
但是,我現在面臨的問題是我不能在枚舉名稱中使用“&”。 我無法控制這些類別名稱,因此無法更改。
為什么不使用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
的類型約束,這使得它對代碼的友好程度降低。
我在自己的項目中做了類似的工作...為了簡潔起見...
用法: 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
AC#標識符必須以下划線,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 NFC規范的附件7,且名稱不能單獨區分大小寫。更嚴格。
簡而言之,你不能。
更重要的是,您為什么要這么做? 你怎么能告訴Gas&Fuel
從標識符Gas&Fuel
具有表達Gas
和Fuel
作為操作數到&
運營商?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.