我是否以良好的方式返回通用类型

时间:2014-11-18 09:19:12

标签: c# generics

我试图返回Generic类型,我遇到的问题是,当我在调试时,当我尝试检查返回的值时应用程序失败。

所以,如果我有

public class BaseRepository : IBaseRepository
{
    public BaseRepository(IDbContext context)
    {
        this.Context = context;
    }

    protected IDbContext Context { get; private set; }

    public TEntity Get<TEntity>(Guid id) where TEntity : class
    {
        IDbSet<TEntity> entitySet = this.Context.Set<TEntity>();
        return entitySet.Find(id);
    }
}

我尝试做

var entity = studentRepository.Get<Student>(...)

当我设置一个断点时,我无法检查实体的值,然后我的应用程序失败了。函数和调用是在2个不同的程序集中,如果这可能与问题有关。

我收到了这条消息:

  

无法获得本地或参数&#39;实体的价值。因为它不是   在这个指令指针处可用,可能是因为它已经存在   优化了。

然后w3wp.exe崩溃

所以我想我没有使用那种通用类型,因为我应该

因此,可以在存储库中包含此类Get,而无需在BaseRepository中指定通用类型,如BaseRepository<TEntity>

1 个答案:

答案 0 :(得分:0)

我已经安装了 Framework 4.5.2 并解决了这个问题。 cf

reference