转换为通用的基类不起作用

时间:2020-07-16 09:44:49

标签: c# generics

我有课:

[Serializable]
public class SaveObject
{
   public Dictionary<Type, List<BaseModel>> Tables { get; set; }
}

和尝试放入新列表的方法

public List<T> GetCollection<T>() where T : BaseModel
{
    if (saveObject.Tables == null)
        saveObject.Tables = new Dictionary<Type, List<BaseModel>>();

    List<T> repo = saveObject.Tables.FirstOrDefault(e => e.Key == typeof(T)).Value as List<T>;
    if (repo == null)
    {
        repo = new List<T>();
        saveObject.Tables.Add(typeof(T), repo);
    }

    return repo;
}

问题是: 无法从“ System.Collections.Generic.List ”转换为“ System.Collections.Generic.List

enter image description here

我不明白为什么无法将T转换为基本模型,而在这里我注意到T是基本模型。

0 个答案:

没有答案
相关问题