C# 自动转换接口方法

C# Auto-Casting Interface Methods

我有一个在其签名中使用抽象 class 的接口。我的实现然后在他们的签名中使用抽象 class 的 subclass 但这是不允许的。有人可以帮我吗?我在谷歌上只能找到通用接口和抽象 class 实现...

public MyAbstractClass {
    abstract public void myMethod();
}

public MySubClass : MyAbstractClass {
    override public void myMethod(){
        ...
    }
}

然后我有我的 interface/implementation..

public interface MyInterface {
    MyAbstractClass myInterfaceMethod(MyAbstractClass blah);
}

public MyImplementation : MyInterface {
    MySubClass myInterfaeMethod(MySubClass blah){
        ...
    }
}

但是我在构建时遇到错误,说 myInterfaceMethod 没有实现接口方法...

帮忙?

你不能那样做,因为它违反了接口。但是,您可以使 MyInterface 通用。

public interface MyInterface<T> where T: MyAbstractClass {
    T myInterfaceMethod(T blah);
}

public MyImplementation : MyInterface<MySubClass> {
    MySubClass myInterfaeMethod(MySubClass blah){
        ...
    }
}