我已阅读相关问题(How to get around lack of covariance with IReadOnlyDictionary?),但我不明白它如何帮助解决我的问题。
这是Box
可以编辑(使用IBoxTarget
)并通知更改(使用IBoxSource
)的内容:
interface IBoxTarget
{
void DoSomething();
}
interface IBoxSource
{
event Action SomethingIsDone;
}
class Box : IBoxTarget, IBoxSource
{
public void DoSomething()
{
// . . . some logic . . .
if (SomethingIsDone != null) SomethingIsDone();
}
public event Action SomethingIsDone;
}
Room
是一个容器Box
。它还实现了两个接口:
interface IRoomTarget
{
IReadOnlyDictionary<string, IBoxTarget> Boxes { get; }
}
interface IRoomSource
{
IReadOnlyDictionary<string, IBoxSource> Boxes { get; }
}
class Room : IRoomTarget, IRoomSource
{
Dictionary<string, Box> boxes = new Dictionary<string, Box>();
IReadOnlyDictionary<string, IBoxTarget> IRoomTarget.Boxes
{
get { return boxes; } // Error: "Cannot implicitly convert type ..."
}
IReadOnlyDictionary<string, IBoxSource> IRoomSource.Boxes
{
get { return boxes; } // Error: "Cannot implicitly convert type ..."
}
}
我不想在里面创建两个不同的字典Room
。我需要做什么?