473,378 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 4058
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daniel | last post by:
when loading this dll into an assembly via late binding how do i diferentiate between outer CAsmToLoad class and the inner class CAsmToLoad inside of CAsmToLoad2? public class CAsmToLoad {...
4
by: Christopher Ireland | last post by:
Hi -- I'm trying to find an example of a nested class implemented within the .NET Framework itself but without much success. I don't suppose that anybody knows of such an example off the top of...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
9
by: ex-neo-con | last post by:
I'd like to write code to make a nested class publicly available, but I want to restrict its instantiation to instances of the containing class. I know I could do such a thing using reflection,...
1
by: Raju Joseph | last post by:
Hi All, We are in the process of developing an N-Tier app using VB.NET. We are extensively using classes (entity objects) in our design. Further, most of the times, we do have to specify nested...
0
by: GBR | last post by:
Hi Guys, I have been boggling over this for the past few days. I have a class like this: Class A Property Name as String Property ID as Integer Property Address as B End Class
2
by: bill | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types contained in a nested stucture in C#? Once I get the list of MemberInfo and determine...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
11
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.