2012-01-12 23 views
0

私はいくつかのファイルを消費したいので、私はそれぞれのデータをどこに置くべきかを知る必要があります。 1列は「カテゴリ」と呼ばれます。"&"は列挙型ですか?

私は多くのカテゴリを持つことができ、それらをサポートするために列挙型を構築したいと考えています。

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を使用して維持しています。私はこれが最善の方法であるかどうかはわかりません(私はいつもより良いアイデアに向けて開いています)。

しかし、今直面している問題は、「&」を列挙型の名前で使用できないことです。私はこれらのカテゴリ名を制御することができないので、変更することはできません。

+11

[BooksAndSuppliesなどの] "と"についてはどうでしょうか? –

+0

[C#で列挙型の数値を使用する]重複している可能性があります(http://stackoverflow.com/questions/3916914/c-sharp-using-numbers-in- an-enum) –

+0

@BobKaufman - 私は名前を制御できません。私は列挙型を解析しようとする前に、置き換えを行い、 "&"があるかどうかを確認して "And"にします。 – chobo2

答えて

3

なぜ説明タグを使用しませんこの問題を回避するにはそれが何のために設計されたのか正確には認められませんが、あなたがそれを問題にするなら、あなた自身の属性クラスを常に創造することができます。

ここで私はここで

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(); 
    } 

オブジェクトのdescription属性を取得するために書いた迅速な汚い方法は、リストなどのオブジェクトのすべての記述子のリストを取得するための迅速な汚いユーティリティメソッドです

今効果にこれらを利用するためにあなたが

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) 
     { 

     } 
    } 

が必要

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; 
} 

Iジェネリックスマスターではありません。誰かが、歓迎されるGetDescriptions()メソッドのタイピングの問題を回避する方法についていくつかのアイデアを持っている場合。残念ながら、私は予想した型制約を置くことができませんでした。

1

私は自分のプロジェクトでこのような何かをやった...簡潔にするために編集した...

USAGE:GetType(Operators).GetTitle(tName))

IMPLEMENTATION:

<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 
+0

+1属性を使用してソリューションを提供する –

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は、NFCに標準化されたhttp://www.unicode.org/reports/tr15/tr15-18.htmlアネックス7に従う識別子を必要とし、さらに厳密に区別されます。

要するに、できません。

さらに、なぜあなたはしたいですか? Gas&FuelGas&Fuelの識別子にGasFuelという表現を&オペレータのオペランドとして指定するにはどうすればよいですか?