Reflection

R

Rlrcstr

If I have some code behind a form and I want to be able to pass an object to
it... any of several user defined classes that I have... and then display
the values in the member variables of the class, I'm guessing that I use
Reflection. I have never used anything like that before. Can someone point
me to a good example of doing this? I will basically be drawing a table on
the form with dta from the passed class in columns. The different classes
have different numbers and types of data, so I need to inspect the class at
run time and determine data types and values.

Thanks.

Jerry
 
C

Chris Dunaway

Have you looked at the PropertyGrid? It already does what you propose.
You supply it with an object and it displays its properties and
values.
 
R

Rlrcstr

I saw that, but I'm actually doing a bit more with the info - dynamically
creating other objects based on the types and values passed, etc... Just
knew that if I could do what I previously described, it would give me the
info I needed to go further. But I think I discovered how to do it and,
turns out, it's not all that complicated. Thanks for the response.

Jerry
 
K

Ken Tucker [MVP]

Hi,

Test class



Public Class MyTestClass

Dim mstrA As String

Dim mstrB As String

Dim mintC As Integer

Public Property A() As String

Get

Return mstrA

End Get

Set(ByVal Value As String)

mstrA = Value

End Set

End Property

Public Property B() As String

Get

Return mstrB

End Get

Set(ByVal Value As String)

mstrB = Value

End Set

End Property

Public Property C() As Integer

Get

Return mintC

End Get

Set(ByVal Value As Integer)

mintC = Value

End Set

End Property

End Class



To get values



Dim c As New MyTestClass

c.A = "a"

c.B = "B"

c.C = 100

Dim t As Type = c.GetType

For Each pi As Reflection.PropertyInfo In t.GetProperties

Debug.WriteLine(String.Format("{0} {1} {2}", pi.Name, _

pi.PropertyType.FullName, pi.GetValue(c, Nothing)))

Next



Ken

----------------------
If I have some code behind a form and I want to be able to pass an object to
it... any of several user defined classes that I have... and then display
the values in the member variables of the class, I'm guessing that I use
Reflection. I have never used anything like that before. Can someone point
me to a good example of doing this? I will basically be drawing a table on
the form with dta from the passed class in columns. The different classes
have different numbers and types of data, so I need to inspect the class at
run time and determine data types and values.

Thanks.

Jerry
 
P

Phill. W

Rlrcstr said:
If I have some code behind a form and I want to be able to pass
an object to it... any of several user defined classes that I have...
and then display the values in the member variables of the class,
I'm guessing that I use Reflection.

I wouldn't.

If you just want to be able to "Print" your classes (say for debugging),
override the ToString method and return something "meaningful".
This can be accessed from absolutely anywhere one of these objects
goes.

If you want to be able to work with these classes, then you /don't/
want to be minutely examining each object to see whether it's got
"this" method or "that" property. You could go looking for /your/
classes, though, as in

Class Class1
Public Sub Beep()
End Sub
End Class

then

Public Sub BeepAnObject( ByVal thing as Object )
If TypeOf thing Is Class1 Then
Dim c1Thing as Class1 _
= DirectCast( thing, Class1 )

c1Thing.Beep()

End If
End Sub

Getting more complex still, use Inheritance or Interfaces to create
a common way in which the classes can be used. For example,
*every* class that implements the Interface IDisposable /must/
by definition, have a Dispose method that can be called.
Using an Interface:

Public Interface IBeepable
Sub Beep()
End Interface

Public Class Class2
Implements IBeepable

Public Sub Beep() Implements IBeepable.Beep
End Sub

End Class

then

Public Sub BeepAnObject( ByVal thing as IBeepable )
thing.Beep()
End Sub

HTH,
Phill W.
 
R

Rlrcstr

Phil,

Your feedback is appreciated. I have a few questions, though...

First of all, why do most people push to avoid using reflection? Seems to
me that it provides some very cool abilities.

With my objects, I'm not checking to see if specific props exist, I'm just
iterating through the props and creating a custom listbox with columns of
the appropriate type for property.

I suppose I could create an interface with a method that provides an array
of the column names and types and a method to return the value of property
by name, but wouldn't I end up using reflection in the object to return the
value of the requested property? Unless I hard code a select statement for
object I create...

Does reflection add a lot of overhead? Just trying to figure out why I
wouldn't use it. Thanks.

Jerry
 
C

Cor Ligthert

Jerry,

Reflection is late binding.

Something the same as using option strict off
(Finding the right class for the object in runtime)

You can see it like this in performance

VB6 and VBNet with Option Strict off is the same
C# and VBNet with Option Strict on (avoiding late binding) is the same.

This are not absolute figurs, because if somebody uses Option Strict off,
however has no late binding in its program the result will be of course as
with Option Strict On.

There are as well situations where VB6 can be faster than C# or VBNet or C++
however that is not in general. (To avoid a message from Herfried).

:)

It is just to give you an idea.

Cor
 
P

Phill. W

.. . .
First of all, why do most people push to avoid using reflection?
Seems to me that it provides some very cool abilities.

"Cool" perhaps, but a potentially sizeable overhead at runtime.
And there are better (i.e.more /reliable/) ways of doing this sort of
thing that come in the O.O. Bag of Goodies.
That said, I have calls to reflection.MethodBase.GetCurrentMethod()
at the top and tail of all of my functions, so the overhead isn't /that/
big.
With my objects, I'm not checking to see if specific props exist, I'm
just iterating through the props and creating a custom listbox with
columns of the appropriate type for property.

If you /really/ want to be able to do this with /any/ class, then yes;
Reflection's the way to go.
I suppose I could create an interface with a method that provides
an array of the column names and types and a method to return
the value of property by name, but wouldn't I end up using
reflection in the object to return the value of the requested property?

If you created an Interface, you'd be defining a /common/ set of
properties, etc. that /every one/ of your classes would have.
There'd be no "room" for adding more.
Unless I hard code a select statement for object I create...

Errk!!

HTH,
Phill W.
 
R

Rlrcstr

Again, thanks for the info...

So it really sounds like Reflection might be the way to go with this.
Unless you can enlighten me to some of the other (better? more reliable?)
ways of doing this...

I'm creating a control that is, in effect, an ownerdraw listbox where each
each item in the list is displayed with columns for all the properties of
the object. (At this point, strings or boolean.)

I add columns to the listbox and specify the name and type of the property
that will be displayed in that column. Then add a bunch of objects that
expose those properties to the list. The list then, using reflection, pulls
each property by name from the object from the info supplied when I defined
the columns. Info is displayed appropriately (text or checkbox).

How else would you do this and keep the architecture as flexible? Thanks.

Jerry
 

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