C#中的继承树和受保护的构造函数 [英] Inheritance trees and protected constructors in C#

查看:134
本文介绍了C#中的继承树和受保护的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于下面的继承树,以可行的方式实现它的最佳方法是什么?

Given the following inheritance tree, what would be the best way of implementing it in a way that works?

abstract class Foo<T> : IEnumerable<T>
{
    public abstract Bar CreateBar();
}

class Bar<T> : Foo<T>
{
    // Bar's provide a proxy interface to Foo's and limit access nicely.
    // The general public shouldn't be making these though, they have access
    // via CreateBar()
    protected Bar(Foo base)
    {
        // snip...
    }
}

class Baz<T> : Foo<T>
{
    public Bar CreateBar()
    {
        return new Bar(this);
    }
}

此操作失败,并显示:'Bar.Bar()' is inaccessible due to its protection level.

This fails with: 'Bar.Bar()' is inaccessible due to its protection level.

我不希望构造函数是公共的,只有继承自Foo的类才能够创建Bar. Bar是专用的Foo,任何类型的Foo都应能够创建一个.公共内部在这里是一个选项",因为Foo的大多数预定义扩展都将在DLL的内部,但是我认为这是一个草率的答案,因为后来出现的任何人都想创建自己的FooBaz(可能会发生)将被默认的CreateBar()实现卡住,这可能满足或可能无法满足他们的需求.

I don't want the constructor being public, only classes that inherit from Foo should be able to create Bars. Bar is a specialised Foo, and any type of Foo should be able to create one. Public internal is an 'option' here, as the majority of the predefined extensions to Foo will be internal to the DLL, but I consider this a sloppy answer, since anyone who comes along later who wants to create their own type of Foo or Baz (which is likely to happen) will be stuck with a default CreateBar() implementation, which may or may not meet their needs.

也许有一种方法可以使其重构以使其正常工作?我正在尝试在墙上砸头,尝试进行设计以使其能够正常工作.

Perhaps there is a way of refactoring this to make it work nicely? I'm banging my head on the wall trying to design this so it'll work though.

编辑(更多信息):

稍微具体一点:Foo正在实现IEnumerable,并且长话短说,Bar正在提供相同的接口,但只限于该可枚举对象的子集.所有Foo都应该能够创建自己的子集(例如Bar)并将其返回.但是我不想让每个想要实现Foo的人都为此而担心,因为Bar会做代理并且担心限制范围等.

Slightly more concrete: Foo is implementing IEnumerable and long story short, Bar is providing the same interface, but to a limited subset of that enumerable object. All Foo's should be able to create subsets of themselves (ie. Bar) and return it. But I don't want to have everyone who ever wants to implement a Foo to have to worry about this, because Bar will do the proxying and worry about limiting the range, etc.

推荐答案

好的,新答案:

  1. 将Bar拆分为一个界面和一个具体的类.
  2. 用IBar表示公共抽象方法.
  3. 在Foo中使Bar成为私有嵌套类,实现IBar.给它一个内部构造函数,您可以从Foo调用它.
  4. 在Foo中编写一个受保护的方法,该方法从自身创建Bar的实例.如果仅代理就足够了,则从Foo派生的类可以使用此方法来实现抽象方法,而具有更复杂需求的类可以直接实现IBar.您甚至可以将抽象方法更改为虚拟方法,并默认从"this"创建一个新的Bar.

对此的一种变体是使Bar成为Foo中受保护的嵌套类,并带有公共构造函数.这样,任何派生类都将能够自己实例化它,但是任何不相关的类都将根本无法看到"它.您仍然需要将接口与实现分开(以便接口可以是公共的),但是我认为这还是一件好事.

One variant on this would be to make Bar a protected nested class within Foo, with a public constructor. That way any derived class would be able to instantiate it for themselves, but no unrelated class would be able to "see" it at all. You'd still need to separate the interface from the implementation (so that the interface can be public) but I think that's a good thing anyway.

这篇关于C#中的继承树和受保护的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆