|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Get value from nested class using reflectionI am using reflection to read the values of properties from a class. The class is returned from a Web Service so I have to access the class using FieldInfo (Using VS 2003 which converts the Properties into Fields when it comes out of the Web Service). I have this at the moment: Private _aDataSource As Object 'Person class Dim aFieldInfo as FieldInfo = _aDataSource.GetType.GetField("FirstName") Dim aValue As Object = _aFieldInfo.GetValue(_aDataSource) aValue ="Steve" Using this, I can get the FirstName field value from the class, but I want to read the values from a nested Class as well. For example, I have a class called Person, and in that class is another class called Address. I want to be able to read the value of AddressLine1 from the Address class within the Person class using reflection. I can do the following to get the Address class: Dim aFieldInfo as FieldInfo = _aDataSource.GetType.GetField("Address") but if I try to access the fields on that class then I get an exception about Object types not being able to be converted to target types (or something like that!). So, using reflection, how can I access the value of the field AddressLine1 from the Address class that is in the Person class? Thank you for any help. Kind Regards, Steve Steve Amey wrote:
> I am using reflection to read the values of properties from a class. The Am I missing something here, or have you not got a Proxy object (that > class is returned from a Web Service so I have to access the class using > FieldInfo (Using VS 2003 which converts the Properties into Fields when it > comes out of the Web Service). you use to /call/ the Web Service) that contains the definitions for the class[es] that gets /returned/ by the web service? These types would be published in the Web Service's WSDL and should be interpreted into the Proxy when you add a Web Reference to it. (I think; I've not done a /huge/ amount with Web Serivces, bu I've never had to reach into the Reflection toolbox - yet). HTH, Phill W. Hiya
I am using reflection because I am binding controls to the properties of an object based on the control's Tag and Name. Here is a very cut-down snippett of code to give you an idea For Each c As Control In Me.Controls Dim aFieldInfo as FieldInfo = _aDataSource.GetType.GetField(c.Name.Substring(3)) Dim aValue As Object = _aFieldInfo.GetValue(_aDataSource) c.Text = aValue Next Eg. Form bound to a class called Person. TextBox on form called txtFirstName is bound to the property FirstName in the class. TextBox with Tag Address and name txtAddressLine1 will be bound to the AddressLine1 property of the Address class that is within the Person class. In order to do this, I need to read the AddressLine1 property (or should I say field as it's from a web service) of the Address class within the Person class. Regards, Steve Show quoteHide quote "Phill W." wrote: > Steve Amey wrote: > > > I am using reflection to read the values of properties from a class. The > > class is returned from a Web Service so I have to access the class using > > FieldInfo (Using VS 2003 which converts the Properties into Fields when it > > comes out of the Web Service). > > Am I missing something here, or have you not got a Proxy object (that > you use to /call/ the Web Service) that contains the definitions for the > class[es] that gets /returned/ by the web service? > These types would be published in the Web Service's WSDL and should be > interpreted into the Proxy when you add a Web Reference to it. > (I think; I've not done a /huge/ amount with Web Serivces, bu I've never > had to reach into the Reflection toolbox - yet). > > HTH, > Phill W. > Steve Amey wrote:
> Eg. Form bound to a class called Person. TextBox on form called txtFirstName I've never used it, but can't you do that with Data Binding?> is bound to the property FirstName in the class. TextBox with Tag Address and > name txtAddressLine1 will be bound to the AddressLine1 property of the > Address class that is within the Person class. Perhaps not, because you get a whole /new/ object from the Web Service each time (rather than the same instances having its bits changed). Failing that, have a look at CallByName - it's quick and dirty and VB6-ish, but you can use it to read any property on any Object, purely by name. HTH, Phill W.
Querying a database using vb
Want to know the power-2 based numbers of x OpenGL Refresh problem using CsGL with VB.NET How to disable the UAC in Vista? Resolving Type mismatch, HOW Deriving Combobox items indexes bypass security in Outlook How to keep statusStrip at the bottom of the ToolStripContainer Sub(Thing as Object) {thing = new Thing.GetType} ? GPS and VB .net 2005 |
|||||||||||||||||||||||