|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dynamically Determining an Objects PropertyI 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? Yes, you could use reflection for that.
http://msdn2.microsoft.com/en-us/library/system.type.getproperty.aspx -- Show quoteTim 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? > > 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? > > > > > > > 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 -- Show quoteTim Wilson ..NET Compact Framework MVP "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? > > > > > > > > > > > > |
|||||||||||||||||||||||