C# 如何重写此获取泛型存储库以使用泛型的方法

C# 如何重写此获取泛型存储库以使用泛型的方法,c#,generics,C#,Generics,我使用此方法从字典中获取通用存储库: public readonly IDictionary<Type, IRepository> _repositories = new Dictionary<Type, IRepository>(); public IRepository GetRepository(Type type) { if (this._repositories.ContainsKey(type)) { return this._rep

我使用此方法从字典中获取通用存储库:

public readonly IDictionary<Type, IRepository> _repositories = new Dictionary<Type, IRepository>();

public IRepository GetRepository(Type type)
{
    if (this._repositories.ContainsKey(type)) {
        return this._repositories[type];
    }
    return null;
}
public readonly IDictionary\u repositories=new Dictionary();
公共IRepository GetRepository(类型)
{
if(此._repositories.ContainsKey(类型)){
返回此项。_存储库[类型];
}
返回null;
}
这是可行的,但我希望它能使用泛型,所以我尝试:

public IRepository<T> GetRepository<T>() where T : class
{
    var typeParameterType = typeof(T);

     if (this._repositories.ContainsKey(typeParameterType)) {
         return this._repositories[typeParameterType];
     }
     return null;
}
public IRepository GetRepository(),其中T:class
{
var-typeParameterType=typeof(T);
if(this._repositories.ContainsKey(typeParameterType)){
返回此项。_存储库[typeParameterType];
}
返回null;
}
但是我得到了一个错误,比如“无法将type
IRepository
隐式转换为
IRepository
”。存在显式转换(是否缺少强制转换?)


有人知道如何解决这个问题吗?

将您的返回类型从
IRepository
更改为simply
IRepository
,以匹配原始非通用方法的返回类型。

将您的返回类型从
IRepository
更改为simply
IRepository
,以匹配原始非通用方法的返回类型方法。

返回类型应该是
IRepository
(注意缺少
),因为IRepository在字典的定义中不是泛型的。

返回类型应该是
IRepository
(注意缺少
),因为IRepository在字典的定义中不是泛型的。

错误提示,您需要将
GetRepository
的返回类型更改为非泛型
IRepository

public IRepository GetRepository<T>() where T : class
{
    var typeParameterType = typeof(T);

    if (this._repositories.ContainsKey(typeParameterType)) 
        return this._repositories[typeParameterType];

    return null;
}
或者可能更合适:

public IRepository<T> GetRepository<T>() where T : class
{
    Repository<T> rep = null;

    this._repositories.TryGetValue(typeof(T), out rep);

    return rep;
}
public IRepository GetRepository(),其中T:class
{
rep=null;
这个.u repositories.TryGetValue(typeof(T),out rep);
返回代表;
}

如错误所示,您需要将
GetRepository
的返回类型更改为非泛型
IRepository

public IRepository GetRepository<T>() where T : class
{
    var typeParameterType = typeof(T);

    if (this._repositories.ContainsKey(typeParameterType)) 
        return this._repositories[typeParameterType];

    return null;
}
或者可能更合适:

public IRepository<T> GetRepository<T>() where T : class
{
    Repository<T> rep = null;

    this._repositories.TryGetValue(typeof(T), out rep);

    return rep;
}
public IRepository GetRepository(),其中T:class
{
rep=null;
这个.u repositories.TryGetValue(typeof(T),out rep);
返回代表;
}

您的词典有一种类型的IRepository。因此,当您提取一个元素时,它也具有这种类型。另一方面,您的方法希望您返回类型为
IRepository
的值

有两种方法可以解决这个问题。将方法的返回类型更改为IRepository,或者在返回元素之前将其转换为
IRepository

return (IRepository<T>)this._repositories[typeParameterType];
返回(IRepository)此存储库[typeParameterType];

您的词典有一种类型的IRepository。因此,当您提取一个元素时,它也具有这种类型。另一方面,您的方法希望您返回类型为
IRepository
的值

有两种方法可以解决这个问题。将方法的返回类型更改为IRepository,或者在返回元素之前将其转换为
IRepository

return (IRepository<T>)this._repositories[typeParameterType];
返回(IRepository)此存储库[typeParameterType];