C# 非泛型方法&x27;字典<;类型,类型>;。添加(类型,类型)和#x27;不能与类型参数一起使用
我有下面的类和接口实现C# 非泛型方法&x27;字典<;类型,类型>;。添加(类型,类型)和#x27;不能与类型参数一起使用,c#,.net,repository-pattern,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 c
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
}
}
公共接口IPageRepository:IRepository
{
IList GetPage(标准,out int totalRows);
}
公共接口ICategoryRepository:IPageRepository
{
//其余的方法
}
公共类CategoryRepository:BaseDap,ICategoryRepository
{
//方法实现
}
公共类存储工厂
{
私有静态只读字典容器=新字典();
静态存储工厂()
{
container.Add();
//在此处获取错误:
//非泛型方法“Dictionary.Add(Type,Type)”不能与类型参数一起使用
}
}
我在其他项目中也遵循了类似的模式,而且它在那里工作。
我在这里遗漏了什么?您将类型参数与实际参数混淆了。你本想写的
container.Add(typeof(ICategoryRepository), typeof(CategoryRepository));
您将类型参数与实际参数混淆了。你本想写的
container.Add(typeof(ICategoryRepository), typeof(CategoryRepository));
如果给出的答案解决了你的问题,请接受答案-如果给出的答案解决了你的问题,请接受答案-这对我很有效。但我在另一个项目中有类似的模式。它没有给出任何错误。为什么?@Lokesh:“相似”意味着“不同”。不同的是,你在不同的程序中编写的不同代码是正确的,而你在这个程序中编写的代码是错误的。这对我很有用。但我在另一个项目中有类似的模式。它没有给出任何错误。为什么?@Lokesh:“相似”意味着“不同”。不同之处在于,您在不同程序中编写的不同代码是正确的,而在此程序中编写的代码是错误的。