简体   繁体   English

无法从集合中推断出通用类型

[英]Unable to infer generic type from a collection

Why is it not possible to do this: 为什么不可能这样做:

public static void Init<TEntity>(params TEntity[] repositories) where TEntity : Entity
{
    foreach (TEntity item in repositories)
    {
       Repository<item> rep = new Repository<item>();
    }
}

The above code won't compile: Cannot resolve symbol item 上面的代码无法编译: 无法解析符号项目

Yet this works: 但这有效:

public static void Init<TEntity>(TEntity entity) where TEntity : Entity
{
    Repository<TEntity> rep = new Repository<TEntity>();
}

EDIT 编辑

I'm editing the OP to give a bigger picture of the problem. 我正在编辑OP,以更全面地了解问题。 We are experiencing some issues with the entity framework Db Context being spawned by multiple repositories. 我们正在遇到由多个存储库产生的实体框架Db Context的一些问题。 Currently we access the repositories like: 当前,我们访问以下存储库:

Repository<Product> rep = new Repository<Product>()
Repository<Account> rep = new Repository<Account>()

Since there's a relationship between product and account EF will complain about objects being attached to a different context. 由于产品和帐户之间存在关系,EF会抱怨对象被附加到不同的上下文中。 So we are trying to resolve this issue by consolidating repository access into a Unit of Work pattern: 因此,我们正在尝试通过将存储库访问合并为工作单元模式来解决此问题:

Here's an example of what I'm trying to achieve: 这是我要实现的目标的一个示例:

public class UnitOfWork
{
    protected List<Entity> Repositories = new List<Entity>();
    private readonly DbContext _context;
    protected UnitOfWork()
    {
       _context = new SqlDbContext();
    }

    public static UnitOfWork Init<TEntity>(params TEntity[] repositories) where TEntity : Entity
    {
            UnitOfWork uow = new UnitOfWork();
        foreach (TEntity item in repositories)
        {
            Repository<item> rep = new Repository<item>();
            uow.Repositories.Add(rep);
        }
            return uow;
    }

    public IRepository<T> GetRepository<T>() where T : Entity
    {
        return Repositories.OfType<T>().Single();
    }   
}

So we can access our repositories like: 这样我们就可以访问我们的存储库,例如:

GetRepository<Product>().GetById(1);
GetRepository<Account>().GetById(123434);

item是类型的实例 ,但是您需要一个type参数来创建通用Repository<T>类的实例。

Not sure what makes you try the first case ( Repository<item> rep = new Repository<item>(); ). 不知道是什么让您尝试第一种情况( Repository<item> rep = new Repository<item>(); )。 Here, 'item' is an instance and not a type. 在这里,“ item”是实例而不是类型。 If you need a genertic item your second case does it best. 如果您需要一件大件物品,那么第二件事情最好。

Please elaborate your 'problem'. 请详细说明您的“问题”。

Edit: If this question is out of curiosity, then you may take a look at 'MakeGenericType) method, following code explains the usage. 编辑:如果这个问题出于好奇,那么您可以看看'MakeGenericType)方法,以下代码说明了用法。

       Type generic = typeof(Repository<>);

       Type[] typeArgs = { item.GetType() };

       Type constructed = generic.MakeGenericType(typeArgs);
// This will create Repository<item> type.
    public static UnitOfWork Init<T1>()
    {
        UnitOfWork uow = new UnitOfWork();
        uow.Add(new Repository<T1>());
        return uow;
    }

    public static UnitOfWork Init<T1,T2>()
    {
        UnitOfWork uow = Init<T1>();
        uow.Add(new Repository<T2>());
        return uow;
    }

    public static UnitOfWork Init<T1, T2, T3>()
    {
        UnitOfWork uow = Init<T1,T2>();
        uow.Add(new Repository<T3>());
        return uow;
    }

    // ...

That's incorrect syntax. 那是不正确的语法。 Item is an instance of type TEntity. Item是类型TEntity的实例。 You don't use instance variables with generic definitions. 您不要将实例变量与泛型定义一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM