469,625 Members | 1,138 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

Get value from nested class using reflection

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
Sep 22 '06 #1
3 3865
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.
Sep 26 '06 #2
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." 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.
Sep 26 '06 #3
Steve Amey wrote:
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.
Sep 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Christopher Ireland | last post: by
8 posts views Thread by Robert W. | last post: by
1 post views Thread by Raju Joseph | last post: by
reply views Thread by GBR | last post: by
4 posts views Thread by =?Utf-8?B?Qnlyb24=?= | last post: by
11 posts views Thread by Yarco | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.