【问题标题】:Casting issue with generic list通用列表的铸造问题
【发布时间】:2012-07-08 13:20:14
【问题描述】:

在下面的代码中出现编译错误但我不明白为什么。

class RealMock<TEntity> : DataContext
{
    public RealMock():base("")
    {

    }

    public List<TEntity> inMemoryDataStore = new List<TEntity>();
    public List<TEntity> GetTable<TEntity>() where TEntity : class
    {
        return inMemoryDataStore;  //Compilation error       
    }
}

无法将类型“System.Collections.Generic.List [c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\mscorlib.dll]”隐式转换为“System.Collections.Generic” .List [c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\mscorlib.dll]'

【问题讨论】:

    标签: .net list casting


    【解决方案1】:

    我相信这是因为您的 GetTable 方法已参数化,请尝试将其定义为

    public List<TEntity> GetTable () {...}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      相关资源
      最近更新 更多