10

Java では、追加データを含む列挙型を簡単に記述できました。
私はそれを次のように説明することができます

public enum OperatorType
{
    GreaterOrEqual  (">=", "GreaterOrEqual"),
    Greater         (">" ,"Greater"),
    Less            ("<", "Less"),
    LessOrEqual     ("<=", "LessOrEqual"),
    Equal           ("==", "Equal"),
    Between         ("Between", "Between"),
    Around          ("Around","Around");

    private final String symbol;
    private final String name;

    private OperatorType(final String symbol, final String name) {
        this.symbol = symbol;
        this.name = name;
    }
}

次に、values() を反復処理する静的メソッドを追加し、すべてのデータをハッシュマップに追加し、属性の 1 つをキーとしてマップから完全な列挙型データを取得できるようにします。

簡単に言えば、enum は Java で非常に発達した型です。

さて、
C# に移行しますが、私のオプションは何ですか?
列挙型とその属性を保持し、それをマップにロードし、必要に応じてキーで取得したいと考えています。支援することはありますか (各列挙型のシングルトーンなど、これは良い考えではありません)。
ありがとう。

4

5 に答える 5

2

属性を作成できます:

public class EnumKeyAttribute : Attribute
{
    public string Key { get; set; }

    public string Description { get; set; }

    public EnumKeyAttribute(string key, string description)
    {
        this.Key = key;
        this.Description = description;
    }
}

次に、それを列挙型に適用します

public enum OperatorType
{
    [EnumKey(">=", "GreaterOrEqual")]
    GreaterOrEqual,

    [EnumKey(">", "Greater")]
    Greater,

    [EnumKey("<", "Less")]
    Less,

    [EnumKey("<=", "LessOrEqual")]
    LessOrEqual,

    [EnumKey("==", "Equal")]
    Equal,

    [EnumKey("Between", "Between")]
    Between,

    [EnumKey("Around", "Around")]
    Around
}

属性データを取得するには、リフレクションを使用できます。以下は、「Less」の属性を取得する例です。

        MemberInfo memberInfo = typeof(OperatorType).GetMember(OperatorType.Less.ToString()).FirstOrDefault();

        if(memberInfo != null)
        {
            EnumKeyAttribute attribute = (EnumKeyAttribute)memberInfo.GetCustomAttributes(typeof(EnumKeyAttribute), false).FirstOrDefault();

            Console.WriteLine(attribute.Key);
            Console.WriteLine(attribute.Description);
        }

ただし、これらの列挙型は実行時に作成されないため、ディクショナリで値を検索する静的メソッドを作成することで効率を上げることができます。使いやすくするための拡張メソッドとしてこれを行います

public static class KeyFinder
{
    private static Dictionary<OperatorType, EnumKeyAttribute> lookupTable =
        new Dictionary<OperatorType, EnumKeyAttribute>();

    public static EnumKeyAttribute GetKey(this OperatorType type)
    {

        if (lookupTable.ContainsKey(type))
        {
            return lookupTable[type];
        }

        MemberInfo memberInfo = typeof(OperatorType).GetMember(type.ToString()).FirstOrDefault();

        if (memberInfo != null)
        {
            EnumKeyAttribute attribute = (EnumKeyAttribute)memberInfo.GetCustomAttributes(typeof(EnumKeyAttribute), false).FirstOrDefault();

            if (attribute != null)
            {
                lookupTable.Add(type, attribute);
                return attribute;
            }
        }

        // add a null value so next time it doesn't use reflection only to find nothing
        lookupTable.Add(type, null);

        return null;
    }
}

したがって、値を取得するには、次のようにします。

OperatorType.Less.GetKey().Key
OperatorType.Less.GetKey().Description

null 参照の例外に注意してください (属性が見つからない場合は null を返すため)。キーで検索したい場合は、文字列値をキーとして使用する他の拡張メソッドを作成するだけです。

于 2012-11-18T22:59:49.447 に答える