非泛型方法'Dictionary <type,type =“”>。Add(Type,Type)'不能与类型参数一起使用

时间:2018-10-12 06:16:39

标签: c# .net repository-pattern

我有下面的类和接口实现

public interface IPageRepository<T> : IRepository
{
    IList<T> GetPage(Criteria criteria, out int totalRows);
}

public interface ICategoryRepository : IPageRepository<Category>
{
    // rest of the methods
}

public class CategoryRepository : BaseDap, ICategoryRepository
{
    // method implementations
}

public class RepositoryFactory
{

    private static readonly Dictionary<Type, Type> container = new Dictionary<Type, Type>();

    static RepositoryFactory()
    {
        container.Add<ICategoryRepository, CategoryRepository>();  

        //Getting error here:
        //The non-generic method 'Dictionary<Type, Type>.Add(Type, Type)' cannot be used with type arguments
    }
}

我在其他项目中也遵循了类似的模式,并且在那里可以正常工作。 我在这里想念什么?

1 个答案:

答案 0 :(得分:5)

您已经将类型参数与实际参数混淆了。你打算写

 container.Add(typeof(ICategoryRepository), typeof(CategoryRepository));