繁体   English   中英

如何实现具有接口属性的实现?

[英]How to impliment an implement which has an interface property?

我想要一个 class 具有另一个 class 的属性,但我希望外部和内部类都实现一个接口,外部接口具有内部接口的属性。

这是我尝试过的代码。

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; }
}

但我不希望这样做,因为这意味着一旦有多个这样的属性,代码就会变得非常混乱。

任何人都知道任何其他解决方法?

class MainBody : IMainBody 
{
    public SubProperty subProperty { get; set; }
    ISubProperty IMainBody.subProperty { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM