.NET 类访问修饰符(friend 和 public)

发布于 2024-09-26 06:36:06 字数 1735 浏览 3 评论 0原文

我正在开发一个将在多个项目中使用的类库。

在我的类库中,我有一个“形状”类,它具有许多属性。 这些属性之一是“尺寸”,返回具有“高度”、“宽度”和“深度”属性的类。

如何禁止 Dimension 类在编辑器中查看,同时在类库中免费提供?

我已将命令放入类文件中,但这会将其隐藏在库和我的应用程序中。

<ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)> _

我还将类访问修饰符更改为 Friend,但这会阻止在类库外部调用时对 Shape 类中的 Property 进行公共访问。

我想要做的就是防止在类库之外创建 Dimension 类的实例。

谢谢。

这是我想要实现的代码功能:

Interface IShape

    ReadOnly Property Properties() As ShapeProperties

End Interface

Public Class Shape

    Implements IShape

    Dim _Properties As New ShapeProperties(0, 0, 0)

    Sub New()
        _Properties = New ShapeProperties(3, 4, 5)
    End Sub

    Public ReadOnly Property Properties() As ShapeProperties Implements IShape.Properties
        Get
            Return _Properties
        End Get
    End Property

End Class

Friend Class ShapeProperties

    Dim _Height As Integer = 0
    Dim _Width As Integer = 0
    Dim _Depth As Integer = 0

    Friend Sub New(ByVal h As Integer, ByVal w As Integer, ByVal d As Integer)
        _Height = h
        _Width = w
        _Depth = d
    End Sub

    Private ReadOnly Property Height() As Integer
        Get
            Return _Height
        End Get
    End Property
    Private ReadOnly Property Width() As Integer
        Get
            Return _Width
        End Get
    End Property
    Private ReadOnly Property Depth() As Integer
        Get
            Return _Depth
        End Get
    End Property

End Class

我无法编译上面代码中的类库。我希望 ShapeProperties 只能在 Shape 类中访问,而不是单独访问。我能够解决此问题的唯一方法是更改​​ ShapeProperties 的访问属性,这不是我想要的。

ClassLibrary.Shape 可以,但是 ClassLibrary.Properties 不是。

谢谢。

I am developing a class library that will be used in several projects.

In my class library, I have a "Shape" Class which has a number of properties.
One of these properties is "Dimensions" returns a class with "Height" "Width" and "Depth" properties.

How would I suppress the Dimension class from being viewable in the editor, whilst freely avilable in the class library?

I've put a command in the class file, but this hides it in the library and my application.

<ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)> _

I have also changed the class acess modifier to Friend, but this prevents Public access to the Property in the Shape class when called outside the class library.

All I want to do is prevent creating an Instance of the Dimension class outside the class library.

Thanks.

This is the code functionality I like to achieve:

Interface IShape

    ReadOnly Property Properties() As ShapeProperties

End Interface

Public Class Shape

    Implements IShape

    Dim _Properties As New ShapeProperties(0, 0, 0)

    Sub New()
        _Properties = New ShapeProperties(3, 4, 5)
    End Sub

    Public ReadOnly Property Properties() As ShapeProperties Implements IShape.Properties
        Get
            Return _Properties
        End Get
    End Property

End Class

Friend Class ShapeProperties

    Dim _Height As Integer = 0
    Dim _Width As Integer = 0
    Dim _Depth As Integer = 0

    Friend Sub New(ByVal h As Integer, ByVal w As Integer, ByVal d As Integer)
        _Height = h
        _Width = w
        _Depth = d
    End Sub

    Private ReadOnly Property Height() As Integer
        Get
            Return _Height
        End Get
    End Property
    Private ReadOnly Property Width() As Integer
        Get
            Return _Width
        End Get
    End Property
    Private ReadOnly Property Depth() As Integer
        Get
            Return _Depth
        End Get
    End Property

End Class

I could not compile the Class Library in the above code. I want ShapeProperties to be only accessible in the Shape class, not on its own. The only way I was able to resolve this was to change the access property of ShapeProperties which is not what I want.

ClassLibrary.Shape is OK but
ClassLibrary.Properties is not.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫言歌 2024-10-03 06:36:06

您可以创建一个公共接口 (IShape) 并创建一个实现它的私有类。

You could make a public interface (IShape) and make a private class that implements it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文