Get value from nested class using reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Amey

    #1

    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.Ge tType.GetField( "FirstName" )
    Dim aValue As Object = _aFieldInfo.Get Value(_aDataSou rce)
    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.Ge tType.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


  • Phill W.

    #2
    Re: Get value from nested class using reflection

    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.

    Comment

    • Steve Amey

      #3
      Re: Get value from nested class using reflection

      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.Ge tType.GetField( c.Name.Substrin g(3))
      Dim aValue As Object = _aFieldInfo.Get Value(_aDataSou rce)
      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.
      >

      Comment

      • Phill W.

        #4
        Re: Get value from nested class using reflection

        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.

        Comment

        Working...