0

我有一堂课如下

Public Class Foo
    Private _Name As String
    <ShowInDisplay()> _
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Private _Age As String
    Public Property Age() As String
        Get
            Return _Age
        End Get
        Set(ByVal value As String)
            _Age = value
        End Set
    End Property

    Private _ContactNumber As String
    <ShowInDisplay()> _
    Public Property ContactNumber() As String
        Get
            Return _ContactNumber
        End Get
        Set(ByVal value As String)
            _ContactNumber = value
        End Set
    End Property
End Class

我只需要处理那些具有特定属性的属性,例如:ShowInDisplay

Public Sub DisplayOnlyPublic(ByVal Someobject As Foo)
    For Each _Property As something In Someobject.Properties
        If _Property.HasAttribute("ShowInDisplay") Then  
           Console.WriteLine(_Property.Name & "=" & _Property.value)
        End If
    Next
End Sub
4

2 回答 2

2

编辑:使用正确的 VBGetType()调用更新:

If _Property.IsDefined(GetType(ShowInDisplayAttribute), True) Then
于 2009-02-10T08:07:30.190 回答
0

除了能够使用扩展方法/lambda(无论如何在 c# 中)使其更好之外,没有比在每个可用属性上使用 MemberInfo.IsDefined 更简单的方法了。

于 2009-02-10T08:09:09.233 回答