短い答えはノーです。
インターフェイスがある場合IMyInterface<T>
、コンパイラは、使用する T の型ごとに新しいインターフェイスを作成し、T のすべての値を置き換えます。
例を挙げると:
私はインターフェースを持っています:
public interface IMyInterface<T> {
public T Value {get; set;}
}
私は2つのクラスを持っています:
public class Foo {}
public class Bar : Foo {}
次に、次のように定義します
public class Orange : IMyInterface<Foo> {}
public class Banana : IMyInterface<Bar> {}
コンパイラは、特定の命名規則を使用して 2 つの新しいインターフェイスを自動的に作成します。異なる名前を使用して、それらが異なることを強調します。
public interface RandomInterface {
Foo Value { get; set; }
}
public interface AlternativeInterface {
Bar Value { get; set; }
}
public class Orange : RandomInterface {
}
public class Banana : AlternativeInterface {
}
ご覧のとおり、 と の間RandomInterface
に関係はありませんAlternativeInterface
。したがって、から継承するクラスRandomInterface
はキャストできませんAlternativeInterface
質問のコメントを読んだ後に更新
IRepository を期待する関数に MockRepository を渡したい場合は、次のようにします。
public void MyFunction<T>(IRepository<T> repo) where T: IIdentifiable {
}