Iterating through class properties.

N

Nick

Hi all,

Just a quick question. I have a class that exposes a number of fields (which
are themselves custom types) through public properties.

At run time, I have an object whom I'd like to check, is of same type as one
of these fields.

Is there a method or technique for iterating through a class's public
properties, such as maybe using something from the reflection namespace?

This would probably be similar to iterating through the
System.Drawing.SystemColors class's properties.


Regards,


Nick
 
H

Herfried K. Wagner [MVP]

Nick,

Nick said:
Just a quick question. I have a class that exposes a number of fields
(which are themselves custom types) through public properties.

At run time, I have an object whom I'd like to check, is of same type as
one of these fields.

Is there a method or technique for iterating through a class's public
properties, such as maybe using something from the reflection namespace?

\\\
Public Class Foo
Private m_Property1 As String
Private m_Property2 As String
Private m_Property3 As Integer

Public Property Property1() As String
Get
Return m_Property1
End Get
Set(ByVal Value As String)
m_Property1 = Value
End Set
End Property

Public Property Property2() As String
Get
Return m_Property2
End Get
Set(ByVal Value As String)
m_Property2 = Value
End Set
End Property

Public Property Property3() As Integer
Get
Return m_Property3
End Get
Set(ByVal Value As Integer)
m_Property3 = Value
End Set
End Property
End Class

Private Sub SetAllStringPropertiesToFoo(ByVal Obj As Object)
For Each p As PropertyInfo In Obj.GetType().GetProperties( _
BindingFlags.Instance Or BindingFlags.Public _
)
If p.PropertyType Is GetType(String) Then
p.SetValue(Obj, "Foo", Nothing)
End If
Next p
End Sub
..
..
..
Dim foo As New Foo
With foo
.Property1 = "X"
.Property2 = "Y"
.Property3 = 22
SetAllStringPropertiesToFoo(foo)
MsgBox(.Property1)
MsgBox(.Property2)
MsgBox(.Property3.ToString())
End With
///
 
N

Nick

Excellent. Thanks :)


Herfried K. Wagner said:
Nick,



\\\
Public Class Foo
Private m_Property1 As String
Private m_Property2 As String
Private m_Property3 As Integer

Public Property Property1() As String
Get
Return m_Property1
End Get
Set(ByVal Value As String)
m_Property1 = Value
End Set
End Property

Public Property Property2() As String
Get
Return m_Property2
End Get
Set(ByVal Value As String)
m_Property2 = Value
End Set
End Property

Public Property Property3() As Integer
Get
Return m_Property3
End Get
Set(ByVal Value As Integer)
m_Property3 = Value
End Set
End Property
End Class

Private Sub SetAllStringPropertiesToFoo(ByVal Obj As Object)
For Each p As PropertyInfo In Obj.GetType().GetProperties( _
BindingFlags.Instance Or BindingFlags.Public _
)
If p.PropertyType Is GetType(String) Then
p.SetValue(Obj, "Foo", Nothing)
End If
Next p
End Sub
.
.
.
Dim foo As New Foo
With foo
.Property1 = "X"
.Property2 = "Y"
.Property3 = 22
SetAllStringPropertiesToFoo(foo)
MsgBox(.Property1)
MsgBox(.Property2)
MsgBox(.Property3.ToString())
End With
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top