简体   繁体   English

返回具有公共基类的不同类型的列表

[英]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.

相关问题 具有静态列表的C#基类在实例化类型之间是否有所不同? - C# Base class with static list to be different between instantiated types? 在给定的程序集中按通用基类分组类型 - Group types by common base class in given assembly 将几种不同类型包装在通用类中以进行通用访问? - Wrapping several different types in a common generic class for common access? 如果对象具有相同的基数 class,如何处理具有不同返回类型的方法? - How to handle methods with different return types if the objects have same base class? 从类型集合中获取公共基类的最简单方法 - Easiest way to get a common base class from a collection of types 实现接口或基础 class 的类型列表 - List of Types that implement an interface or base class 如何将不同类型的子元素反序列化为基本类型的列表/集合,这是相应 class 的属性 - How to deserialize child elements of different types into list/collection of base type which is a property of the corresponding class 基类还是普通类? - Base class or common class? 如何创建将返回公共接口派生类型的工厂类 - How to create factory class which will return the derived types of common interface 基类的相同方法的不同返回类型 - Different return type for same method of a base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM