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

Mas*_*ter 0 c# inheritance dictionary list

我有一个基类如下

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

    protected Dictionary<string, List<somethingThatWillChange>> myDic{ get; set; }
Run Code Online (Sandbox Code Playgroud)

但是,这个类将有两个将继承它的类.其中一个继承类需要new Dictionary<string, List<Type1>>(),另一个需要new Dictionary<string, List<Type2>>().

Type1和Type2是类,Type1有7个字段(名称,年龄,时间,工作,汽车,工资,标题),Type2有3个字段(名称,年龄,时间).

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

然后在两个继承的类中,我想初始化或将其转换为适当的List<type1>List<type2>

  • 我不想声明多个词典.
  • 我不想在我的Type1和Type2类上继承

有没有办法做到这一点?

And*_*ena 6

使用泛型:

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

    protected Dictionary<string, List<T>> myDic{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)