1

私は次の列挙型を持っています:

public enum QuestionType {
    Check = 1,
    CheckAndCode = 2,
    na = 99
};

public static class QuestionTypeExtension
{
    public static string D2(this QuestionType key)
    {
        return ((int) key).ToString("D2");
    }
}

出力をフォーマットする拡張メソッドを既に作成しましたが、別の要件があります。私がする必要があるのは、列挙型の内容を次のクラスのリストに返す拡張メソッドを作成することです。

public class Reference {
   public string PartitionKey { get; set; } // set to "00"
   public int RowKey { get; set; } // set to the integer value
   public string Value { get; set; } // set to the text of the Enum
}

拡張メソッドでこれを行うことは可能ですか?

4

2 に答える 2

2

次のことを試してください。

public static List<Reference> GetReferencesForQuestionType()
{
    return Enum.GetValues(typeof(QuestionType))
        .Cast<QuestionType>()
        .Select(x => new Reference
                         {
                             PartitionKey = "00", 
                             RowKey = (int)x, 
                             Value = x.ToString()
                         })
        .ToList();
}

Reference拡張メソッドの 1 つの要素に対してのみ -classのインスタンスを作成する場合は、次のようにします。

public static Reference ToReference(this QuestionType questionType)
{
    return new Reference
                     {
                         PartitionKey = "00", 
                         RowKey = (int)questionType, 
                         Value = questionType.ToString()
                     };
}    
于 2012-10-23T09:02:17.993 に答える
1

どうですか...

public static class QuestionTypeExtension
{
    public static IEnumerable<Reference> Reference()
    {
        return Enum.GetValues(typeof(QuestionType)).OfType<QuestionType>().
            Select(qt=>new Reference(){ PartitionKey = "00", RowKey = (int)qt, Value = qt.ToString()});
    }
} 
于 2012-10-23T09:12:53.420 に答える