Newbie with Reflection Issues: Class vs. Instance

M

MDBloemker

Based on the answers to my first post, I've spent some time boning up on
Reflection. Everything I find has Reflection being used against a class, but
what I'm looking for is some way to do the same thing, but against an
instance< of the class, such as SRObj as an instance of the cls_SR class.
The ultimate goal being to be able to do the following:

mvar = marray(3) ' array element holding a ColumnName, such as SRType

SRObj(mvar) = datareader(mvar) ' SRObj.Member = datareader.ColumnnameValue
where the name of the Member is the same as the ColumnName, such as SRType

The datareader part works fine. The object part insists on a default
property, but when I give it one, it insists on assigning the value of the
datareader column only to the PropertyName part of the function (that is,
Default Property Whatsis(ByVal mvar as String) as String returns the value
of the column SRType into the instance's Whatsis member. Close, but no
cigar.) I feel as though I'm missing an important step, or even a whole
concept. Does Reflection work at all on a class instance? Is my thinking
even in the right place??

MDBloemker
(e-mail address removed)
 
R

Rick Spiewak

Here is an example using reflection which relies on the instance:

Public Function GetPropertyByName(ByVal PropertyName As String) As Object
Dim BusinessObjectProperty As PropertyInfo
BusinessObjectProperty =
Me.GetType.GetProperty(PropertyName.ToLower)
If BusinessObjectProperty Is Nothing Then Throw New
ArgumentException("Property Not Found")
Return BusinessObjectProperty.GetValue(Me, Nothing)
End Function

And, calling this property:

With (dg)
' Handle business object collections here by use of the base
types
If .DataSource.GetType.BaseType Is GetType(BusinessObjects) Then
If .DataSource.count = 0 Then
sval = "No items found"
Return sval
End If
If .DataSource.count <= dg.PageSize Then
sval = String.Empty 'No footer needed for only one
page
Return sval
End If
sval = DirectCast(.DataSource(offset),
BusinessObject).GetPropertyByName(field).ToString
Return sval
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top