是否有可能解决接口继承中使用的传递闭包?

Is there any possibility to work around the transitive closure used in interface inheritance?

使用接口继承,我想在终端 interface/class 中拥有 all 个来自 all 个祖先的项目]而且我还想为所有派生的 interfaces/objects(继承树根)提供一个 基本接口 ,用于像 Process(IBase b) 这样的一般对象处理。所以,例如,而不是这个:

public interface IBase 
{
    Guid Id {get;} 
    void SwitchOn();
}

public interface IPart1 : IBase {void DoPart1Specific();}
public interface IPart2 : IBase {void DoPart2Specific();}
public interface ICompound1 : IPart1, IPart2 {}

public class Compound : ICompound1
{
    public Guid Id => Guid.Empty;        // IBase
    public void SwitchOn() {}            // IBase
    public void DoPart1Specific() {}     // IPart1
    public void DoPart2Specific() {}     // IPart2
}

我想要这样的东西(使用伪显式接口实现符号当然在这里行不通):

public class Compound : ICompound1
{
    Guid Part1.Id => Guid.Empty;         // ICompound1.IPart1
    void Part1.SwitchOn() {}             // ICompound1.IPart1
    void DoPart1Specific() {}            // ICompound1.IPart1

    Guid Part2.Id => Guid.Empty;         // ICompound1.IPart2
    void Part2.SwitchOn() {}             // ICompound1.IPart2
    void DoPart2Specific() {}            // ICompound1.IPart2
}

我能想到的唯一不太好的部分解决方案是复制每个接口定义中的所有常见内容,这太冗长且容易出错(在这种情况下,显式实现有效,让我们说 Compound class 成员不能是 public) 并不重要,但是没有可用的基础接口)o:

public interface IPart1Ex
{
    Guid Id {get;} 
    void SwitchOn();
    void DoPart1Specific();
}

public interface IPart2Ex
{
    Guid Id {get;} 
    void SwitchOn();
    void DoPart2Specific();
}

public interface ICompound1Ex : IPart1Ex, IPart2Ex {}

public class CompoundEx : ICompound1Ex
{
    Guid IPart1Ex.Id => Guid.Empty;
    void IPart1Ex.SwitchOn() {}
    void IPart1Ex.DoPart1Specific() {}

    Guid IPart2Ex.Id => Guid.Empty;
    void IPart2Ex.SwitchOn() {}
    void IPart2Ex.DoPart2Specific() {}
}

看来你根本不想从接口继承,而是使用组合。您的 Compound class 需要为 Part1 保存一个实例,为 Part2 保存一个实例。这会给出类似的东西:

public interface IPart {
    Guid Id { get; }
    void SwitchOn();
    void Execute();
}

public class Compound
{
    private readonly IPart _part1;
    private readonly IPart _part2;

    public Compound(IPart part1, IPart part2)
    {
        _part1 = part1;
        _part2 = part2;
    }

    public Guid Part1Id { get { return _part1.Id; } }
    public void Part1SwitchOn() { _part1.SwitchOn(); }
    public void DoPart1Specific() { _part1.Execute(); }

    public Guid Part2Id { get { return _part2.Id; } }
    public void Part2SwitchOn() { _part2.SwitchOn(); }
    public void DoPart2Specific() { _part2.Execute(); }
}

或者更简单的 class 就是:

public class Compound
{
    public Compound(IPart part1, IPart part2)
    {
        Part1 = part1;
        Part2 = part2;
    }

    public IPart Part1 { get; private set; }
    public IPart Part2 { get; private set; }
}

然后在调用代码中使用:

访问它们
var compound = MyMethodWhichCreatesCompound();
var id1 = compound.Part1.Id;
compound.Part2.Execute();
//etc

我认为在接口成员定义上使用 new 关键字可以帮助您:

public interface IBase 
{
    Guid Id {get;} 
    void SwitchOn();
}

public interface IPart1 : IBase
{
    new Guid Id {get;}
    new void SwitchOn();
    void DoPart1Specific();
}

public interface IPart2 : IBase
{
    new Guid Id {get;}
    new void SwitchOn();
    void DoPart2Specific();
}