インスタンスのリストを返すメソッドを設計しています。各インスタンスは実際には異なるデータ型です。
以下は私のドラフトデザインです。推奨事項が必要です
public class abstract Base
{
//DataType is an enum
public abstract DataType Type { get; set; }
public abstract BaseType Value { get; set; }
}
public abstract class BaseType
{
}
public class MyString:BaseType
{
}
public class MyInt:BaseType
{
}
//string for example
public class Type1:Base
{
public override DataType Type
{
get { return DataType.Type1; }
set;
}
public override BaseType Value
{
get { return new MyString("a"); }
set;
}
}
public class Type2:Base
{
public override DataType Type
{
get { return DataType.Type2; }
set;
}
public override BaseType Value
{
//MyInt for example
get { return new MyInt(10); }
set;
}
}
メソッドは
List<Base> GetValues();
呼び出し元はそのようなものを書くことが期待されています
List<Base> values = GetValues();
foreach(var value in values)
{
switch(value.Type)
{
case DataType.MyString:
MyString str = value.Value as MyString;
break;
case DataType.MyInt:
MyInt str = value.Value as MyInt;
break;
}
}
私の質問は、そのための最良の設計は何ですか? ジェネリックをより適切に使用するにはどうすればよいですか?