Home All Groups Group Topic Archive Search About

Dynamically Determining an Objects Property

Author
21 Mar 2006 10:51 PM
Karl Pierburg
I have an object with 30+ exposed properties.  How can I, based on a string
input, retrieve the value of a specific property? 

Here's what I'm doing right now:

Function GetField(ByVal FieldName as String) as String
   Select Case FieldName
       Case "Field1" Return myObj.Field1
       Case "Field2" Return myobj.Field2
  END Select
End Function

There's got to be a better way then this.  Is Reflection what I'm after?

Author
21 Mar 2006 11:08 PM
Tim Wilson
Yes, you could use reflection for that.
http://msdn2.microsoft.com/en-us/library/system.type.getproperty.aspx

--
Tim Wilson
..NET Compact Framework MVP

Show quote
"Karl Pierburg" <KarlPierb***@discussions.microsoft.com> wrote in message
news:027195EF-F3A5-4D2D-9C76-A28111F6556D@microsoft.com...
> I have an object with 30+ exposed properties.  How can I, based on a
string
> input, retrieve the value of a specific property?
>
> Here's what I'm doing right now:
>
> Function GetField(ByVal FieldName as String) as String
>    Select Case FieldName
>        Case "Field1" Return myObj.Field1
>        Case "Field2" Return myobj.Field2
>   END Select
> End Function
>
> There's got to be a better way then this.  Is Reflection what I'm after?
>
>
Author
22 Mar 2006 2:38 PM
Karl Pierburg
Ok...I think I see where this is going, but I'm still having some issues.

I can do the following:

myObj.GetType.GetProperty("FirstName")

This Returns a PropertyInfo object.  But I can't seem to setup the
"GetValue" method to return the actual string.

Where am I going wrong?

KP

Show quote
"Tim Wilson" wrote:

> Yes, you could use reflection for that.
> http://msdn2.microsoft.com/en-us/library/system.type.getproperty.aspx
>
> --
> Tim Wilson
> ..NET Compact Framework MVP
>
> "Karl Pierburg" <KarlPierb***@discussions.microsoft.com> wrote in message
> news:027195EF-F3A5-4D2D-9C76-A28111F6556D@microsoft.com...
> > I have an object with 30+ exposed properties.  How can I, based on a
> string
> > input, retrieve the value of a specific property?
> >
> > Here's what I'm doing right now:
> >
> > Function GetField(ByVal FieldName as String) as String
> >    Select Case FieldName
> >        Case "Field1" Return myObj.Field1
> >        Case "Field2" Return myobj.Field2
> >   END Select
> > End Function
> >
> > There's got to be a better way then this.  Is Reflection what I'm after?
> >
> >
>
>
>
Author
22 Mar 2006 5:31 PM
Tim Wilson
Assuming that you're looking for a public property of type string, you can
do something like this...

Dim info As PropertyInfo = Me.Button1.GetType().GetProperty("Text")
If (Not info Is Nothing) Then
  Dim obj As Object = info.GetValue(Me.Button1, Nothing)
  If (TypeOf obj Is String) Then
    MessageBox.Show(Convert.ToString(obj))
  End If
End If

--
Tim Wilson
..NET Compact Framework MVP

Show quote
"Karl Pierburg" <KarlPierb***@discussions.microsoft.com> wrote in message
news:7F2B0A4F-CE05-4A5A-9B1C-F5EB2021301E@microsoft.com...
> Ok...I think I see where this is going, but I'm still having some issues.
>
> I can do the following:
>
> myObj.GetType.GetProperty("FirstName")
>
> This Returns a PropertyInfo object.  But I can't seem to setup the
> "GetValue" method to return the actual string.
>
> Where am I going wrong?
>
> KP
>
> "Tim Wilson" wrote:
>
> > Yes, you could use reflection for that.
> > http://msdn2.microsoft.com/en-us/library/system.type.getproperty.aspx
> >
> > --
> > Tim Wilson
> > ..NET Compact Framework MVP
> >
> > "Karl Pierburg" <KarlPierb***@discussions.microsoft.com> wrote in
message
> > news:027195EF-F3A5-4D2D-9C76-A28111F6556D@microsoft.com...
> > > I have an object with 30+ exposed properties.  How can I, based on a
> > string
> > > input, retrieve the value of a specific property?
> > >
> > > Here's what I'm doing right now:
> > >
> > > Function GetField(ByVal FieldName as String) as String
> > >    Select Case FieldName
> > >        Case "Field1" Return myObj.Field1
> > >        Case "Field2" Return myobj.Field2
> > >   END Select
> > > End Function
> > >
> > > There's got to be a better way then this.  Is Reflection what I'm
after?
> > >
> > >
> >
> >
> >

AddThis Social Bookmark Button