[英]Return List of Different Types with common base class
I'm designing a method that should return a List of instances, each instance is actually of a different data type. 我正在设计一种应返回实例列表的方法,每个实例实际上是不同的数据类型。
The following is my draft design, I need a recommendation for it 以下是我的设计稿,我需要一个建议
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;
}
}
Method should be 方法应该是
List<Base> GetValues();
Caller is expected to write something like that 呼叫者应编写类似的内容
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;
}
}
My question is what is the best design for that? 我的问题是最好的设计是什么? Can I use generics better, how?
我可以更好地使用泛型吗?
I would recommend a generic base class: 我建议一个通用的基类:
public abstract class Base<T>
where T : BaseType
{
public abstract DataType Type { get; }
public abstract T Value { get; set; }
}
Type1 is now: Type1现在是:
public class Type1 : Base<MyString>
{
public override DataType Type
{
get { return DataType.MyString; }
}
public override MyString Value
{
get;
set;
}
}
However, I don't know what GetValues
is. 但是,我不知道什么是
GetValues
。 If it returns a list of values of the same type it should also be generic: 如果它返回相同类型的值列表,则还应该是通用的:
public List<Base<T>> GetValues<T>()
where T : BaseType
{
return theList;
}
If it returns elements of different types you can use another non-generic base class: 如果它返回不同类型的元素,则可以使用另一个非通用基类:
public abstract class Base
{
public abstract DataType Type { get; }
}
public abstract class Base<T> : Base
where T : BaseType
{
public abstract T Value { get; set; }
}
The GetValues
method would be: GetValues
方法将是:
public List<Base> GetValues()
{
return theList;
}
Note that I moved the non-generic part into the non-generic base class so that you can still use the DataType property. 请注意,我将非通用部分移到了非通用基类中,因此您仍然可以使用DataType属性。
You need to cast the values to the respective types in order to access the Value
property: 您需要将值转换为相应的类型才能访问
Value
属性:
List<Base> values = GetValues();
foreach (Base value in values)
{
switch (value.DataType)
{
case DataType.MyString:
MyString myString = value as MyString;
...
case DataType.MyInt:
MyInt myInt = value as MyInt;
...
}
}
It seems that you only use the DataType
property to get information about the type of an object. 似乎您仅使用
DataType
属性来获取有关对象类型的信息。 This is not necessary. 这不是必需的。 You can use the
is
operator: 您可以使用
is
运算符:
foreach (Base value in values)
{
if (value is MyString)
{
MyString myString = value as MyString;
...
}
else if (value is MyInt)
{
MyInt myInt = value as MyInt;
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.