我希望一个类具有另一个类的属性,但是我希望外部类和内部类都实现一个接口,外部接口为内部的类提供一个属性。
这是我试过的密码。
interface IMainBody
{
ISubProperty subProperty { get; set; }
}
interface ISubProperty
{
string somethingHere { get; set; }
}
class MainBody : IMainBody // Error CS0738 'MainBody' does not implement interface member 'IMainBody.subProperty'. 'MainBody.subProperty' cannot implement 'IMainBody.subProperty' because it does not have the matching return type of 'ISubProperty'.
{
public SubProperty subProperty { get; set; }
}
class SubProperty : ISubProperty
{
public string somethingHere { get; set; }
}我知道我需要像这样的通用接口
interface IMainBody<T>
where T : ISubProperty
{
T subProperty { get; set; }
}
interface ISubProperty
{
string somethingHere { get; set; }
}但是我不喜欢这样做,因为这意味着一旦有了这样的多个属性,代码就会变得非常混乱。
有人知道其他的解决办法吗?
发布于 2022-01-13 02:50:41
class MainBody : IMainBody
{
public SubProperty subProperty { get; set; }
ISubProperty IMainBody.subProperty { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}https://stackoverflow.com/questions/70690724
复制相似问题