不能在泛型类型或方法

时间:2017-03-14 12:32:54

标签: c#

public class Account
{
}
public class SpecificationBase<T>
{
}
public class AccountSearch : SpecificationBase<Account>
{
}
public class SearchBase<T> where T : SpecificationBase<T>
{
}
public class Start
{
    public Start()
    {
        new SearchBase<AccountSearch>();
    }
}
  

错误:类型“AccountSearch”不能在通用类型或方法“T”中用作类型参数“SearchBase<T>”。没有从“AccountSearch”到“SpecificationBase<AccountSearch>

的隐式引用转换

2 个答案:

答案 0 :(得分:1)

虽然其他答案事实上是正确的并且会使问题消失,但我认为您正在寻找的是以下内容:

public class Account
{
}
public class SpecificationBase<T>
{
}
public class AccountSearch : SpecificationBase<Account>
{
}
public class SearchBase<T, TModel> where T : SpecificationBase<TModel>
{
}
public class Start
{
    public Start()
    {
        new SearchBase<AccountSearch, Account>();
    }
}

答案 1 :(得分:0)

错误是自我解释的。将限制更改为具体类型以执行有效转换:

public class Account
{
}
public class SpecificationBase<T>
{
}
public class AccountSearch : SpecificationBase<Account>
{
}
public class SearchBase<T> where T : SpecificationBase<Account>
{
}
public class Start
{
    public Start()
    {
        new SearchBase<AccountSearch>();
    }
}

}