views:

191

answers:

2

I have a class as follows

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

I just need to work on only those properties which has a specific attribute eg: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
A: 

Edit: Updated with correct VB GetType() call:

If _Property.IsDefined(GetType(ShowInDisplayAttribute), True) Then
Brannon
GetType() keyword ! ;-)
Cerebrus
If _property.IsDefined(GetType(ShowInDisplay), True) ThenThanks again
Sachin
I am just trying to build a quick reference on reflection at StackOverFlow for others . Thanks for participation
Sachin
A: 

With the exception of being able to make it nicer with extension methods / lambdas (in c# anyway), there is no simpler way than using MemberInfo.IsDefined on each of the available properties.

Richard Szalay