Reflection and PropertyInfo

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.
 
J

Jorge

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
 
O

Oskar Lundgren

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...
 

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