有没有办法使构造函数仅对C#中的父类可见? [英] Is there a way to make a constructor only visible to a parent class in C#?

查看:86
本文介绍了有没有办法使构造函数仅对C#中的父类可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类的集合,这些类继承自我创建的抽象类.我想将抽象类用作创建我的抽象类的具体实现实例的工厂.

I have a collection of classes that inherit from an abstract class I created. I'd like to use the abstract class as a factory for creating instances of concrete implementations of my abstract class.

除了父类之外,是否有任何其他方法可以隐藏所有代码中的构造函数.

Is there any way to hide a constructor from all code except a parent class.

我想基本上做到这一点

public abstract class AbstractClass
{
    public static AbstractClass MakeAbstractClass(string args)
    {
        if (args == "a")
            return new ConcreteClassA();
        if (args == "b")
            return new ConcreteClassB();
    }
}

public class ConcreteClassA : AbstractClass
{
}

public class ConcreteClassB : AbstractClass
{
}

但是我想防止任何人直接实例化这两个具体的类.我想确保只有MakeAbstractClass()方法可以实例化基类.有什么办法吗?

But I want to prevent anyone from directly instantiating the 2 concrete classes. I want to ensure that only the MakeAbstractClass() method can instantiate the base classes. Is there any way to do this?

更新
我不需要从Abstract类的外部访问ConcreteClassA或B的任何特定方法.我只需要Abstract类提供的公共方法.我确实并不需要阻止实例化Concrete类,我只是想避免使用它,因为它们没有提供新的公共接口,只是抽象类内部一些非常具体的东西的不同实现.

UPDATE
I don't need to access any specific methods of ConcreteClassA or B from outside of the Abstract class. I only need the public methods my Abstract class provides. I don't really need to prevent the Concrete classes from being instantiated, I'm just trying to avoid it since they provide no new public interfaces, just different implementations of some very specific things internal to the abstract class.

对我来说,最简单的解决方案是

To me, the simplest solution is to make child classes as samjudson mentioned. I'd like to avoid this however since it would make my abstract class' file a lot bigger than I'd like it to be. I'd rather keep classes split out over a few files for organization.

我想对此没有简单的解决方法...

I guess there's no easy solution to this...

推荐答案

您可以将子类设置为子类,如下所示:

You can make the sub classes child classes, something like this:

public abstract class AbstractClass
{
    public static AbstractClass MakeAbstractClass(string args)
    {
        if (args == "a")
            return new ConcreteClassA();
        if (args == "b")
            return new ConcreteClassB();
    }

    private class ConcreteClassA : AbstractClass
    {
    }

    private class ConcreteClassB : AbstractClass
    {
    }
}

@Vaibhav This does indeed mean that the classes are also hidden. But this is as far as I am aware the only way to completely hide the constructor.

正如其他人所提到的,使用Reflection可以完成相同的事情,它实际上可能更接近于您想要的情况-例如,上述方法将具体类答复为与抽象类,可能不是很方便.话虽如此,这是一个很好的"Hack",如果具体类的数量和复杂性较低,则很好.

As others have mentioned the same thing can be accomplished using Reflection, which might actually be closer to what you would like to be the case - for example the above method replies on the concrete classes being inside the same file as the Abstract class, which probably isn't very convenient. Having said that this way is a nice 'Hack', and good if the number and complexity of the concrete classes is low.

这篇关于有没有办法使构造函数仅对C#中的父类可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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