简体   繁体   English

如何创建一个抽象的类,但不是内部的类

[英]How create a class that is abstract, but not internally

Is this possible in c# somehow? 这有可能在c#中以某种方式吗? This is the exact case I need it for: He (the user) must not be able to create instances of this class. 这是我需要它的确切情况:他(用户)必须无法创建此类的实例。 However, I want to be able to create instances of this class inside my project. 但是,我希望能够在我的项目中创建此类的实例。 (hence: internally) Other classes should be able to inherit from it. (因此:内部)其他类应该能够从中继承。 I want to force the user to only use the bigger Sound class. 我想强制用户只使用更大的Sound类。 The big "BUT": The user must be able to use instances of this class if I give it to him, that's why I can't just make it internal and be done with it. 大“BUT”:如果我将它提供给他,用户必须能够使用这个类的实例,这就是为什么我不能把它作为内部并完成它。 Neither can I make it abstract, because I want to make copies myself. 我也不能把它抽象化,因为我想自己制作副本。 I thought about giving the user only interfaces like IChannel back, but that doesn't solve my problem that he shouldn't be able to make an instance of Channel. 我想过只向用户提供IChannel之类的接口,但这并不能解决我不应该创建Channel实例的问题。 He will still be able to see the class if he looks for it, and if he is adventureous enough even try to create an instance. 如果他找到它,他仍然可以看到课程,如果他足够冒险甚至尝试创建一个实例。 But as I mentioned before: I can't make it abstract because I need to make copies internally. 但正如我之前提到的:我无法将其抽象化,因为我需要在内部制作副本。

Appreciate your help! 感谢您的帮助!

You should be able to declare the constructor as internal and the class itself as public. 您应该能够将构造函数声明为internal,将类本身声明为public。 That should (I think) give you what you want. 应该(我认为)给你你想要的东西。

Shouldn't an internal constructor solve the issue? internal构造函数不应该解决问题吗? That way only your assembly can create instances, but the user can still use instances without restrictions. 这样只有你的程序集可以创建实例,但用户仍然可以不受限制地使用实例。

内部构造函数。

只是让类的构造函数内部?

You can always control instance creation by using different modifiers on the constructor: 您始终可以通过在构造函数上使用不同的修饰符来控制实例创建:

public class MyClass
{
    internal MyClass()
    {
    }
}

Now nobody can instantiate your class, but if you give them an instance they can work with it like normal. 现在没有人可以实例化你的类,但是如果你给他们一个实例,他们可以像平常那样使用它。

As many other people mentioned you can have an internal constructor in a public class. 正如许多其他人提到的,你可以在公共类中拥有一个内部构造函数。

However, I would provide the customer with a public interface and make the whole class internal (or abstract if you want the client to be able to subclass it). 但是,我会为客户提供一个公共接口,并使整个类内部(或者如果您希望客户端能够将其子类化,则为抽象)。 This way they will use the interface, which will make it easier for them to unit test their code. 这样他们就会使用界面,这将使他们更容易对他们的代码进行单元测试。 It will give you flexibility to change the implementation in the future. 它将为您提供在未来更改实施的灵活性。

Using an internally scoped constructor will allow you to create subclasses within the same assembly or any other assembly to which you've made internals visible. 使用内部作用域构造函数将允许您在同一个程序集或任何其他已使内部可见的程序集中创建子类。 It will prevent the creation of subclasses in other assemblies, however, because every class derived from another is required to call a constructor on the base class. 但是,它会阻止在其他程序集中创建子类,因为从另一个派生的每个类都需要在基类上调用构造函数。 If your constructor isn't available based on its scope this will prevent other's from deriving from your class. 如果你的构造函数根据其范围不可用,这将阻止其他人从你的类派生。

Generally, I don't like this kind of thinking, though. 一般来说,我不喜欢这种想法。 As a developer it's really annoying to find that the developer of an API I am using thinks he knows everything about how I want to use it. 作为开发人员,发现我使用的API的开发人员认为他知道我想要如何使用它的一切都非常烦人。 Yes, hide the internal details of the implementation if you must, but please allow me to extend the API to do what I need (and you haven't thought of) and don't make me jump through hoops to do it. 是的,如果必须,请隐藏实现的内部细节,但是请允许我扩展API以执行我需要的操作(并且您没有想到)并且不要让我跳过箍来完成它。

Oh -- and yes, do create an interface for it and use it as the return. 哦 - 是的,为它创建一个接口并将其用作返回。 You'll make my and your life easier in the long run, in addition to making it much easier to mock out your code in my unit tests. 从长远来看,您将使我和您的生活更轻松,此外还可以更轻松地在单元测试中模拟您的代码。

I think what you want is a public class with a protected constructor and an internal factory method: 我想你想要的是一个带有protected构造函数和internal工厂方法的public类:

public class MyClassWhichIsAbstractExceptForMe
{
    // Allow derived classes but prevent public construction.
    protected MyClassWhichIsAbstractExceptForMe()
    {
    }

    // Allow internal construction
    internal static MyClassWhichIsAbstractExceptForMe Create()
    {
        return new MyClassWhichIsAbstractExceptForMe();
    }
}

I also gently recommend a design review. 我也温和地推荐一个设计评论。 This seems quite odd, as others have pointed out. 正如其他人所指出的那样,这似乎很奇怪。

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

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