1

나는 속성 클래스를 가지고 :정말 빈 문자열()을 반환해야합니까?

<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() I 이후뿐만 아니라 인터페이스 속성 위에 배치 DirectoryAttribute의 값을 반환하지 않는다 Type.GetCustomAttributes 메서드의 상속 매개 변수에을 true로 지정 하시겠습니까?

+0

http://hyperthink.net/blog/getcustomattributes-gotcha/ –

답변

2

나는 혼란의 두 가지 주요 포인트 있다고 생각합니다 : 유형에

  1. 귀하의 코드를 찾고 있습니다 속성 및 속성은 속성에 적용됩니다. 속성을 검색하려면 로그인 또는 이름과 같은 속성에서 속성을 검색해야합니다.
  2. 상관없이 상속과 구현간에 차이점이 있습니다. 그룹 그룹 은 IGroup 인터페이스 인을 구현하므로 IGroup에 정의 된 속성이 표시되지 않습니다. 을 상속 받으면 그룹의 속성이 상속됩니다. 유형이 구현하는 인터페이스에 속성을 원하면 인터페이스에 대해 직접 물어보아야합니다. 유형을 거치지 않아야합니다.
관련 문제