1

次のサンプルを取得します。

ここに画像の説明を入力してください

のさまざまなインスタンスがまたはなどである可能性があるため、PreferenceOption呼び出されるプロパティを追加したいと思います。DataTypePreferenceOptionboolstring

これを行う方法はありますか?はいの場合、どのように?

私はのようなものを考えてpublic ValueType DataType { get; set; }いましたが、次のようなインスタンスを作成するときPreferenceOption

PreferenceOption WantsHouse = new PreferenceOption () { PreferenceOption = "Want House?", Weighting = Weighting.Low, Type = bool };

これは機能しませんが、私が何をしたいのかについての良いアイデアを与えるはずです。

助言がありますか?

編集(回答):以下で選択した回答を使用して、現在使用しているものを次に示します(画像がぼやけていることをお詫びします!):

public enum Weighting { One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten }

public class TenantPropertyPreferenceOption<T>
{
    public T PreferenceOption { get; set; }
    public Weighting Weighting { get; set; }
}

public class TenantPropertyPreferenceOptions
{
    TenantPropertyPreferenceOption<bool> WantsHouse = new TenantPropertyPreferenceOption<bool> () { PreferenceOption = false, Weighting = Weighting.One };
    // ...
}
4

3 に答える 3

3

ジェネリック クラスを使用します。

public class PreferenceOption<T>
{
    public T PreferenceOption {get;set;}
    public string PreferenceOptionName {get;set;}
}

PreferenceOption WantsHouse = new PreferenceOption<bool> () { PreferenceOption = true, Weighting = Weighting.Low, PreferenceOptionName ="asd"};

PreferenceOption WantsHouse2 = new PreferenceOption<string> () { PreferenceOption = "this is a string", Weighting = Weighting.Low, PreferenceOptionName="qwe"};
于 2012-12-04T14:02:20.980 に答える
1

タイプを使用

public Type DataType { get; set; }

DataType = typeof(bool)
于 2012-12-04T14:02:33.710 に答える
1

クラスをGenericにすることができます。

PreferenceOption<bool> WantsHouse;
PreferenceOption<string> HouseName;
于 2012-12-04T14:03:00.177 に答える