私は非常に単純なインターフェイスを実装し、それを使用して、次のようなインターフェイスを介してアクセスされるタイプのプロパティにアクセスしようとしています:
interface ITest
{
IOther Other { get; }
}
interface IOther { }
class Other : IOther { }
class Test : ITest
{
public Other Other { get; set; }
}
ただし、次のビルド エラーが発生します。
Error 13 'Test' does not implement interface member 'ITest.Other'. 'Charger.Shared.Test.Other' cannot implement 'ITest.Other' because it does not have the matching return type of 'IOther'.
エラーの意味は理解できましたが、その理由がわかりません。「その他」は「IOther」を実装するので、なぜ問題が発生するのですか?
不完全な解決策は、インターフェイスを明示的に実装することです。
class Test : ITest
{
public Other Other { get; set; }
IOther ITest.Other
{
get { return this.Other; }
}
}
なぜこのボイラープレートが必要なのですか?
ありがとう。
編集:私がこの問題を抱えている実際の非例のコードで、これはオプションではないとして Test を宣言すると仮定します:
class Test : ITest
{
public IOther Other { get; set; }
}