这真的应该返回一个空的 String() 吗?

发布于 2024-10-05 00:52:22 字数 2099 浏览 7 评论 0原文

我有一个属性类:

<AttributeUsage(AttributeTargets.Property)> _
Public Class DirectoryAttribute
    Inherits Attribute

    Private _attribute As String

    Public Sub New(ByVal attribute As String)
        _attribute = attribute
    End Sub

    Public ReadOnly Property Attribute As String
        Get
            Return _attribute
        End Get
    End Property
End Class

以及接口和类:

Public Interface IDirentoryEntity
    <DirectoryAttribute("Name")> _
    Property Name As String
End Interface

Public Interface IOrganizationalUnit
    Inherits IDirectoryEntity
End Interface

Public Class OrganizationalUnit
    Implements IOrganizationalUnit

    ' Implementing IOrganizationalUnit here...'
End Class

Public Interface IIdentifiableDirectoryEntity
    Inherits IDirectoryEntity

    <DirectoryAttribute("sAMAccountName")> _
    Property Login As String
End Interface

Public Interface IGroup
    Inherits IIdentifiableDirectoryEntity
End Interface

Public Class Group
    Implements IGroup

    Private _attributes() As String

    ' Implementing IGroup here...'

    Private ReadOnly Property Attributes() As String()
        Get
            If (_attributes Is Nothing) Then
                Dim attr() As DirectoryAttribute = _
                    CType(GetType(Group).GetCustomAttributes(GetType(DirectoryAttribute), True), DirectoryAttribute())

                If (attr Is Nothing OrElse attr.Length = 0 OrElse attr(0) Is Nothing) Then _
                    Return New String(0) { }

                _attributes = New String(attr.Length) { }

                For index As Integer = 0 To attr.Length - 1 Or _attributes.Length - 1 Step 1
                    _attributes(index) = attr(index).Attribute
                Next
            End If

           Return _attributes
        End Get
    End Property
End Class

话虽如此,Private Property Attributes() As String() 是否应该返回放置在接口上的 DirectoryAttribute 的值属性也是如此,因为我在 Type.GetCustomAttributes 方法的继承参数中指定了 True

I have an attribute class:

<AttributeUsage(AttributeTargets.Property)> _
Public Class DirectoryAttribute
    Inherits Attribute

    Private _attribute As String

    Public Sub New(ByVal attribute As String)
        _attribute = attribute
    End Sub

    Public ReadOnly Property Attribute As String
        Get
            Return _attribute
        End Get
    End Property
End Class

And interfaces and classes:

Public Interface IDirentoryEntity
    <DirectoryAttribute("Name")> _
    Property Name As String
End Interface

Public Interface IOrganizationalUnit
    Inherits IDirectoryEntity
End Interface

Public Class OrganizationalUnit
    Implements IOrganizationalUnit

    ' Implementing IOrganizationalUnit here...'
End Class

Public Interface IIdentifiableDirectoryEntity
    Inherits IDirectoryEntity

    <DirectoryAttribute("sAMAccountName")> _
    Property Login As String
End Interface

Public Interface IGroup
    Inherits IIdentifiableDirectoryEntity
End Interface

Public Class Group
    Implements IGroup

    Private _attributes() As String

    ' Implementing IGroup here...'

    Private ReadOnly Property Attributes() As String()
        Get
            If (_attributes Is Nothing) Then
                Dim attr() As DirectoryAttribute = _
                    CType(GetType(Group).GetCustomAttributes(GetType(DirectoryAttribute), True), DirectoryAttribute())

                If (attr Is Nothing OrElse attr.Length = 0 OrElse attr(0) Is Nothing) Then _
                    Return New String(0) { }

                _attributes = New String(attr.Length) { }

                For index As Integer = 0 To attr.Length - 1 Or _attributes.Length - 1 Step 1
                    _attributes(index) = attr(index).Attribute
                Next
            End If

           Return _attributes
        End Get
    End Property
End Class

With that said, shall the Private Property Attributes() As String() not return the values of DirectoryAttribute placed over the interfaces properties as well, since I specify True in the inheritance parameter of the Type.GetCustomAttributes method?

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

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

发布评论

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

评论(1

诺曦 2024-10-12 00:52:22

我认为有两个主要的混淆点:

  1. 您的代码正在查找类型的属性,并且这些属性应用于属性。如果您想检索属性,则需要在“登录名”或“名称”等属性上查找它们。
  2. 无论如何,继承和实现之间是有区别的。 Group 类实现接口 IGroup,因此当您请求 Group 上的继承属性时,您不会看到在 IGroup 上定义的任何属性。如果您想要类型实现的接口上的属性,则需要直接询问接口,而不能通过类型。

I think there are two main points of confusion:

  1. Your code is looking up attributes on a type, and the attributes are applied to properties. If you want to retrieve the attribute, you'll need to look them up on a property like Login or Name.
  2. Regardless, there's a difference between inheritance and implementation. The class Group implements the interface IGroup, so you won't see any attributes defined on IGroup show up when you ask for inherited attributes on Group. If you want attributes on an interface that a type implements, you'll need to ask about the interface directly, you can't go through the type.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文