是否有可能创建一个只能作为另一个类的成员实例化的类,但仍然可以通过VB.NET中的公共访问?

时间:2023-01-15 16:47:15

I have a custom applicationdata class in this I would like to categorize its data through member subclasses e.g. a userdata class. However the userdata class should not be able to be instantiated other places than in the applicationdata class, but still be accessible through the applicationdata class. Is this possible.

我有一个自定义的applicationdata类,我想通过成员子类对其数据进行分类,例如userdata类。但是,userdata类不能在applicationdata类之外的其他地方实例化,但仍可通过applicationdata类访问。这可能吗。

To illustrate I want to be able to access the class like this: ApplicationData.UserData.SomeProperty

为了说明我希望能够像这样访问类:ApplicationData.UserData.SomeProperty

However outside the ApplicationData class it should not be possible to instance like this: Dim ud as new UserData

但是在ApplicationData类之外,不应该像这样实例:Dim ud as new UserData

4 个解决方案

#1


3  

public class ApplicationData
{
    private UserData user = new UserData();

    public UserData User
    {
        get
        {
            return user;
        }
    }

    public class UserData
    {
        internal UserData()
        {
        }
    }
}

It's not exactly what you describe, in that it's still possible to create a UserData instance outside of your ApplicationData class - but only within the same assembly. Note also that you have to name the class (UserData) differently from the property you're exposing (User).

它并不完全是您描述的内容,因为它仍然可以在ApplicationData类之外创建一个UserData实例 - 但只能在同一个程序集中创建。另请注意,您必须以与要公开的属性(用户)不同的方式命名类(UserData)。

Rather than nest the class like this, the approach I would take instead would be to have a public IUserData interface, and an internal UserData class. Your ApplicationData class can then expose a UserData property of type IUserData.

而不是像这样嵌套类,我将采用的方法是拥有一个公共IUserData接口和一个内部UserData类。然后,您的ApplicationData类可以公开IUserData类型的UserData属性。

#2


0  

The way I would tackle this is as follows: Write a public interface IUserData Make the UserData implement the interface IUserData and make this class private ApplicationData would expose the class through a property named UserData of type IUserData

我将解决这个问题的方法如下:编写一个公共接口IUserData使UserData实现接口IUserData并使该类成为私有ApplicationData将通过名为UserData的属性IUserData公开该类

#3


0  

You can define user data as a abstract class so that it can only be inherited, can not be instanced. For more information see this link

您可以将用户数据定义为抽象类,以便它只能被继承,不能被实例化。有关更多信息,请参阅此链接

Now if UserData and ApplicationData reside in the same assembly, you can user 'internal' keyword. When you use internal keyword to the class, that class in only accessible in the same assembly. The classes which are outside the assembly can not access it. For more information see this link.

现在,如果UserData和ApplicationData位于同一个程序集中,则可以使用“internal”关键字。当您对类使用internal关键字时,该类只能在同一个程序集中访问。程序集外部的类无法访问它。有关更多信息,请参阅此链接。

Cheerss!

Cheerss!

#4


0  

Just use an internal constructor.

只需使用内部构造函数。

No one outside the assembly will be able to create instances.

程序集外部没有人能够创建实例。

#1


3  

public class ApplicationData
{
    private UserData user = new UserData();

    public UserData User
    {
        get
        {
            return user;
        }
    }

    public class UserData
    {
        internal UserData()
        {
        }
    }
}

It's not exactly what you describe, in that it's still possible to create a UserData instance outside of your ApplicationData class - but only within the same assembly. Note also that you have to name the class (UserData) differently from the property you're exposing (User).

它并不完全是您描述的内容,因为它仍然可以在ApplicationData类之外创建一个UserData实例 - 但只能在同一个程序集中创建。另请注意,您必须以与要公开的属性(用户)不同的方式命名类(UserData)。

Rather than nest the class like this, the approach I would take instead would be to have a public IUserData interface, and an internal UserData class. Your ApplicationData class can then expose a UserData property of type IUserData.

而不是像这样嵌套类,我将采用的方法是拥有一个公共IUserData接口和一个内部UserData类。然后,您的ApplicationData类可以公开IUserData类型的UserData属性。

#2


0  

The way I would tackle this is as follows: Write a public interface IUserData Make the UserData implement the interface IUserData and make this class private ApplicationData would expose the class through a property named UserData of type IUserData

我将解决这个问题的方法如下:编写一个公共接口IUserData使UserData实现接口IUserData并使该类成为私有ApplicationData将通过名为UserData的属性IUserData公开该类

#3


0  

You can define user data as a abstract class so that it can only be inherited, can not be instanced. For more information see this link

您可以将用户数据定义为抽象类,以便它只能被继承,不能被实例化。有关更多信息,请参阅此链接

Now if UserData and ApplicationData reside in the same assembly, you can user 'internal' keyword. When you use internal keyword to the class, that class in only accessible in the same assembly. The classes which are outside the assembly can not access it. For more information see this link.

现在,如果UserData和ApplicationData位于同一个程序集中,则可以使用“internal”关键字。当您对类使用internal关键字时,该类只能在同一个程序集中访问。程序集外部的类无法访问它。有关更多信息,请参阅此链接。

Cheerss!

Cheerss!

#4


0  

Just use an internal constructor.

只需使用内部构造函数。

No one outside the assembly will be able to create instances.

程序集外部没有人能够创建实例。