初始化继承 class 中不同对象的列表

initialize list of different objects in inherited class

我有一个基础class如下

    protected BaseClasse()
    {
        this.myDic= new Dictionary<string, List<somethingThatWillChange>>();
    }

    protected Dictionary<string, List<somethingThatWillChange>> myDic{ get; set; }

但是,此 class 将有两个 class 继承自它。其中一个继承的 classes 需要 new Dictionary<string, List<Type1>>() 而另一个需要 new Dictionary<string, List<Type2>>().

Type1 和 Type2 是 classes,Type1 有 7 个字段(姓名、年龄、时间、工作、汽车、薪水、职务),Type2 有 3 个字段(姓名、年龄、时间)。

所以在基础 class 中,我想将我的词典初始化或声明为 new Dictionary<string, List<somethingGeneric>>()

然后在两个继承的classes中,我想初始化或者转换成合适的List<type1>List<type2>

有办法吗?

使用generics:

public BaseClass<T>
{
    protected BaseClasse()
    {
        this.myDic= new Dictionary<string, List<T>>();
    }

    protected Dictionary<string, List<T>> myDic{ get; set; }
}

这应该有效:

public BaseClass<T> {
    protected BaseClass()
    {
        this.myDic = new Dictionary<string, List<T>>();
    }

    protected Dictionary<string, List<T>> myDic { get; set; }
}

public Type1Class : BaseClass<Type1> {
}

public Type2Class : BaseClass<Type2> {
}