从通用接口继承

Mat*_*att 8 c# generics inheritance interface

我不知道如何解决通用接口的问题.

通用接口表示对象的工厂:

interface IFactory<T>
{
    // get created object
    T Get();    
}
Run Code Online (Sandbox Code Playgroud)

接口代表工厂的计算机(计算机类)specyfing一般工厂:

interface IComputerFactory<T> : IFactory<T> where T : Computer
{
    // get created computer
    new Computer Get();
}
Run Code Online (Sandbox Code Playgroud)

通用接口表示对象的特殊工厂,可以克隆(实现接口System.ICloneable):

interface ISpecialFactory<T> where T : ICloneable, IFactory<T>
{
    // get created object
    T Get();
}
Run Code Online (Sandbox Code Playgroud)

类表示计算机(计算机类)和可克隆对象的工厂:

class MyFactory<T> : IComputerFactory<Computer>, ISpecialFactory<T>
{

}
Run Code Online (Sandbox Code Playgroud)

我在MyFactory类中收到编译器错误消息:

The type 'T' cannot be used as type parameter 'T' in the generic type or method 'exer.ISpecialFactory<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'exer.IFactory<T>'.   

The type 'T' cannot be used as type parameter 'T' in the generic type or method 'exer.ISpecialFactory<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'System.ICloneable'.  
Run Code Online (Sandbox Code Playgroud)

Jam*_*iec 9

不确定这是不是一个错字,但应该这样:

interface ISpecialFactory<T>
        where T : ICloneable, IFactory<T>
Run Code Online (Sandbox Code Playgroud)

真的

interface ISpecialFactory<T> : IFactory<T>
        where T : ICloneable
Run Code Online (Sandbox Code Playgroud)

真的,我认为这可能是你想要做的:

public class Computer : ICloneable
{ 
    public object Clone(){ return new Computer(); }
}

public interface IFactory<T>
{
    T Get();    
}

public interface IComputerFactory : IFactory<Computer>
{
    Computer Get();
}

public interface ISpecialFactory<T>: IFactory<T>
    where T : ICloneable
{
    T Get();
}

public class MyFactory : IComputerFactory, ISpecialFactory<Computer>
{
    public Computer Get()
    {
        return new Computer();
    }
}
Run Code Online (Sandbox Code Playgroud)

实例:http://rextester.com/ENLPO67010