Reflection and PropertyInfo

  • Thread starter Thread starter Oskar Lundgren
  • Start date Start date
O

Oskar Lundgren

I want to iterate over fields declared like this, using reflection
(and there are good reasons for that):

Protected WithEvents Foo1 As Bar
Protected WithEvents Foo2 As Bar
....

I use the following code to get an array containing the fields
(wrapped in PropertyInfo objects):

Dim objs As Object() =
Me.GetType().GetProperties(Reflection.BindingFlags.Instance Or
Reflection.BindingFlags.NonPublic)

My question is: How do I get the wrapped "Bar" objects (Foo1, Foo2 and
so on)out of the PropertyInfo objects, so I can make calls to the
methods of the "Bar" objects. Using GetValue on the PropertyInfo
objects does not work.
 
Hello Oskar
See the following example its for MenuItem

<code>
Dim myForm As Type = f.GetType()

Dim fields As FieldInfo() = myForm.GetFields
(BindingFlags.Instance Or BindingFlags.NonPublic)

For Each field As FieldInfo In fields

If field.FieldType.Name = "MenuItem" Then
Dim menu As MenuItem = DirectCast(field.GetValue
(f), MenuItem)
'' you can use it here.
end if
next
</code>

Kind Regards
Jorge
-----Original Message-----
I want to iterate over fields declared like this, using reflection
(and there are good reasons for that):

Protected WithEvents Foo1 As Bar
Protected WithEvents Foo2 As Bar
....

I use the following code to get an array containing the fields
(wrapped in PropertyInfo objects):

Dim objs As Object() =
Me.GetType().GetProperties
(Reflection.BindingFlags.Instance Or
 
The error message I get (at runtime):

Property Get method was not found

This does sound reasonable to me, since I haven't declared such a
method...

Anyway, I have found a solution of my problem. I didn't need to use
reflection, since the Bar class is a subclass System.Web.UI.WebControl
and these objects easily can be retrieved using the Controls collection.
And, yes, I'm new to Visual Basic .NET...
 
Back
Top