Get value from nested class using reflection

G

Guest

Hi all

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).

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
 
P

Phill W.

Steve said:
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.
 
G

Guest

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

Phill W. said:
Steve said:
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.
 
P

Phill W.

Steve said:
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.

I've never used it, but can't you do that with Data Binding?
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.
 

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