Using reflection to get instance name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SmFzb24gUmV5bm9sZHM=?=

    #1

    Using reflection to get instance name

    (using .net 2.0)

    Say you have a class structure like this:

    class Address
    ....
    end class

    class Person
    FirstName
    LastName
    MailingAddress as Address
    BillingAddress as Address
    end class

    I'm using reflection to pull out the values at runtime, but I'm having
    trouble when I dive into an address. When I'm traversing the object I know
    when the type is address and I can get and set values on that instance, but I
    can't figure a way to know if I'm looking at the mailing address or the
    billing address. Is there a way to get the instance name of an object at
    runtime? I've thought about setting a name property in the object, but
    that's a bit of a hack and makes things much less reusable. Thanks in
    advance.
  • =?Utf-8?B?S2VycnkgTW9vcm1hbg==?=

    #2
    RE: Using reflection to get instance name

    Jason,

    Just out of curiosity, what kind of problem are you trying to solve where
    you would need to use reflection in what looks like a typical business
    application?

    Kerry Moorman


    "Jason Reynolds" wrote:
    (using .net 2.0)
    >
    Say you have a class structure like this:
    >
    class Address
    ...
    end class
    >
    class Person
    FirstName
    LastName
    MailingAddress as Address
    BillingAddress as Address
    end class
    >
    I'm using reflection to pull out the values at runtime, but I'm having
    trouble when I dive into an address. When I'm traversing the object I know
    when the type is address and I can get and set values on that instance, but I
    can't figure a way to know if I'm looking at the mailing address or the
    billing address. Is there a way to get the instance name of an object at
    runtime? I've thought about setting a name property in the object, but
    that's a bit of a hack and makes things much less reusable. Thanks in
    advance.

    Comment

    • =?Utf-8?B?SmFzb24gUmV5bm9sZHM=?=

      #3
      RE: Using reflection to get instance name

      Thanks for responding so quickly, Kerry.

      I'm building a library to do two-way databinding of business objects and web
      forms. I'm not going the full scaffolding route, instead simply matching
      property names with control names. I want it to be generic so I can simply
      pass a business object, say Person, and the page control and have binding
      done going in and coming out.



      "Kerry Moorman" wrote:
      Jason,
      >
      Just out of curiosity, what kind of problem are you trying to solve where
      you would need to use reflection in what looks like a typical business
      application?
      >
      Kerry Moorman
      >
      >
      "Jason Reynolds" wrote:
      >
      (using .net 2.0)

      Say you have a class structure like this:

      class Address
      ...
      end class

      class Person
      FirstName
      LastName
      MailingAddress as Address
      BillingAddress as Address
      end class

      I'm using reflection to pull out the values at runtime, but I'm having
      trouble when I dive into an address. When I'm traversing the object I know
      when the type is address and I can get and set values on that instance, but I
      can't figure a way to know if I'm looking at the mailing address or the
      billing address. Is there a way to get the instance name of an object at
      runtime? I've thought about setting a name property in the object, but
      that's a bit of a hack and makes things much less reusable. Thanks in
      advance.

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Using reflection to get instance name

        Jason,
        Is there a way to get the instance name of an object at
        runtime?
        Short answer NO!

        Long answer:

        Instances are not named in .NET per se, variables & fields are named...

        What name would you expect when you do:

        Dim jason As New Person
        Dim nowhere As New Address
        jason.MailingAd dress = nowhere
        jason.BillingAd dress = nowhere

        Remember Address is a reference object, so nowhere, jason.MailingAd dress and
        jason.BillingAd dress all refer to the same instance.
        I've thought about setting a name property in the object, but
        that's a bit of a hack and makes things much less reusable.
        If knowing the difference between a Mailing address & a Billing address is
        important I would either define an AddressType property (an Enum) that had
        the address types on it.

        Enum AddressType
        Billing
        Mailing
        End Enum
        class Address
        ...
        Type As AddressType
        end class

        Or I would make Address abstract (MustInherit) and have MailingAddress &
        BillingAddress inherit from Address.
        class MustInherit Address
        ...
        end class
        class MailingAddress
        Inherits Address
        ...
        end class

        class BillingAddress
        Inherits Address
        ...
        end class
        class Person
        FirstName
        LastName
        MailingAddress as MailingAddress
        BillingAddress as BillingAddress
        end class
        I would use the second if I had "significan t" polymorphic behavior between
        mailing addresses & billing addresses

        --
        Hope this helps
        Jay B. Harlow [MVP - Outlook]
        ..NET Application Architect, Enthusiast, & Evangelist
        T.S. Bradley - http://www.tsbradley.net


        "Jason Reynolds" <JasonReynolds@ discussions.mic rosoft.comwrote in message
        news:3E6CB8E1-B9F2-47F9-82AA-8AE811A1CB8C@mi crosoft.com...
        (using .net 2.0)
        >
        Say you have a class structure like this:
        >
        class Address
        ...
        end class
        >
        class Person
        FirstName
        LastName
        MailingAddress as Address
        BillingAddress as Address
        end class
        >
        I'm using reflection to pull out the values at runtime, but I'm having
        trouble when I dive into an address. When I'm traversing the object I
        know
        when the type is address and I can get and set values on that instance,
        but I
        can't figure a way to know if I'm looking at the mailing address or the
        billing address. Is there a way to get the instance name of an object at
        runtime? I've thought about setting a name property in the object, but
        that's a bit of a hack and makes things much less reusable. Thanks in
        advance.

        Comment

        • =?Utf-8?B?SmFzb24gUmV5bm9sZHM=?=

          #5
          Re: Using reflection to get instance name

          Thanks for your reply, Jay. Adding an enum or abstracting the class isn't
          quite what I'm looking for. Your explanation of instances and reference
          objects is very helpful and crystallized the concept in my mind. Because of
          this I think I'll try another approach. Thanks a bunch for your time.

          "Jay B. Harlow [MVP - Outlook]" wrote:
          Jason,
          Is there a way to get the instance name of an object at
          runtime?
          Short answer NO!
          >
          Long answer:
          >
          Instances are not named in .NET per se, variables & fields are named...
          >
          What name would you expect when you do:
          >
          Dim jason As New Person
          Dim nowhere As New Address
          jason.MailingAd dress = nowhere
          jason.BillingAd dress = nowhere
          >
          Remember Address is a reference object, so nowhere, jason.MailingAd dress and
          jason.BillingAd dress all refer to the same instance.
          >
          I've thought about setting a name property in the object, but
          that's a bit of a hack and makes things much less reusable.
          If knowing the difference between a Mailing address & a Billing address is
          important I would either define an AddressType property (an Enum) that had
          the address types on it.
          >
          Enum AddressType
          Billing
          Mailing
          End Enum
          >
          class Address
          ...
          Type As AddressType
          end class
          >
          >
          Or I would make Address abstract (MustInherit) and have MailingAddress &
          BillingAddress inherit from Address.
          >
          class MustInherit Address
          ...
          end class
          >
          class MailingAddress
          Inherits Address
          ...
          end class
          >
          class BillingAddress
          Inherits Address
          ...
          end class
          >
          class Person
          FirstName
          LastName
          MailingAddress as MailingAddress
          BillingAddress as BillingAddress
          end class
          >
          I would use the second if I had "significan t" polymorphic behavior between
          mailing addresses & billing addresses
          >
          --
          Hope this helps
          Jay B. Harlow [MVP - Outlook]
          .NET Application Architect, Enthusiast, & Evangelist
          T.S. Bradley - http://www.tsbradley.net
          >
          >
          "Jason Reynolds" <JasonReynolds@ discussions.mic rosoft.comwrote in message
          news:3E6CB8E1-B9F2-47F9-82AA-8AE811A1CB8C@mi crosoft.com...
          (using .net 2.0)

          Say you have a class structure like this:

          class Address
          ...
          end class

          class Person
          FirstName
          LastName
          MailingAddress as Address
          BillingAddress as Address
          end class

          I'm using reflection to pull out the values at runtime, but I'm having
          trouble when I dive into an address. When I'm traversing the object I
          know
          when the type is address and I can get and set values on that instance,
          but I
          can't figure a way to know if I'm looking at the mailing address or the
          billing address. Is there a way to get the instance name of an object at
          runtime? I've thought about setting a name property in the object, but
          that's a bit of a hack and makes things much less reusable. Thanks in
          advance.
          >

          Comment

          Working...