【发布时间】:2015-05-26 01:36:16
【问题描述】:
我有两个合同(一个通用接口和另一个非通用)如下:
public interface IGenericContract<T> where T : class
{
IQueryable<T> GetAll();
}
public interface INonGenericContract
{
string GetFullName(Guid guid);
}
我有一个实现两者的类
public class MyClass<T> :
IGenericContract<T> where T : class, INonGenericContract
{
public IQueryable<T> GetAll()
{
...
}
public string GetFullName(Guid guid)
{
...
}
}
一切都很好,直到我编译它。 但是现在当我尝试使用这个类时,我遇到了这个错误 “错误 CS0311:类型 'string' 不能用作泛型类型或方法 'ConsoleApplication1.MyClass' 中的类型参数 'T'。没有从 'string' 到 'ConsoleApplication1.INonGenericContract' 的隐式引用转换。”
class Program
{
static void Main(string[] args)
{
MyClass<string> myClass = new MyClass<string>(); //Error
}
}
如果我不实施非通用合同,它可以正常工作。这里有什么问题?
谢谢
【问题讨论】: