access to member of a class by name;

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • danmath06@gmail.com

    access to member of a class by name;

    If I have an Object and a string wich is the name of a member of the
    object, how can I access the member. I'm trying to do something
    similar to the DisplayMember property the ListBox has. I'm adding some
    functionality to the ListView control, and I would like it to have a
    method that takes a collection of objects and an array of member names
    to know which members to display.
  • Peter Duniho

    #2
    Re: access to member of a class by name;

    On Sat, 08 Nov 2008 17:34:09 -0800, <danmath06@gmai l.comwrote:
    If I have an Object and a string wich is the name of a member of the
    object, how can I access the member. I'm trying to do something
    similar to the DisplayMember property the ListBox has. I'm adding some
    functionality to the ListView control, and I would like it to have a
    method that takes a collection of objects and an array of member names
    to know which members to display.
    You'll need reflection for that. You can call GetType() on the object,
    then call Type.GetMember( ) on that Type instance to retrieve information
    about the actual member, from which you can then do whatever you need to
    get a value for your purposes.

    You might want to consider limiting the feature to properties. That would
    simplify the code doing the reflection, as well as encourage clients of
    your class to encapsulate things better. :) In that case, you can just
    call Type.GetPropert y() instead.

    Pete

    Comment

    • danmath06@gmail.com

      #3
      Re: access to member of a class by name;

      Changed my mind:
      I want to add 2 methods
      1- addcolumn (column_name, width, display member)
      2- additems (collection)

      Comment

      • danmath06@gmail.com

        #4
        Re: access to member of a class by name;

        On Nov 8, 10:46 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
        wrote:
        On Sat, 08 Nov 2008 17:34:09 -0800, <danmat...@gmai l.comwrote:
        If I have an Object and a string wich is the name of a member of the
        object, how can I access the member. I'm trying to do something
        similar to the DisplayMember property the ListBox has. I'm adding some
        functionality to the ListView control, and I would like it to have a
        method that takes a collection of objects and an array of member names
        to know which members to display.
        >
        You'll need reflection for that. You can call GetType() on the object,
        then call Type.GetMember( ) on that Type instance to retrieve information
        about the actual member, from which you can then do whatever you need to
        get a value for your purposes.
        >
        You might want to consider limiting the feature to properties. That would
        simplify the code doing the reflection, as well as encourage clients of
        your class to encapsulate things better. :) In that case, you can just
        call Type.GetPropert y() instead.
        >
        Pete
        thanks pete.

        I tried this as a test and it worked:

        private void getmember (object o, string member){

        Type type = o.GetType();

        System.Reflecti on.PropertyInfo info =
        type.GetPropert y("Nombre");


        MessageBox.Show (info.GetValue( o,null).ToStrin g());

        }

        Comment

        • danmath06@gmail.com

          #5
          Re: access to member of a class by name;

          On Nov 8, 11:07 pm, danmat...@gmail .com wrote:
          On Nov 8, 10:46 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
          wrote:
          >
          >
          >
          On Sat, 08 Nov 2008 17:34:09 -0800, <danmat...@gmai l.comwrote:
          If I have an Object and a string wich is the name of a member of the
          object, how can I access the member. I'm trying to do something
          similar to the DisplayMember property the ListBox has. I'm adding some
          functionality to the ListView control, and I would like it to have a
          method that takes a collection of objects and an array of member names
          to know which members to display.
          >
          You'll need reflection for that. You can call GetType() on the object,
          then call Type.GetMember( ) on that Type instance to retrieve information
          about the actual member, from which you can then do whatever you need to
          get a value for your purposes.
          >
          You might want to consider limiting the feature to properties. That would
          simplify the code doing the reflection, as well as encourage clients of
          your class to encapsulate things better. :) In that case, you can just
          call Type.GetPropert y() instead.
          >
          Pete
          >
          thanks pete.
          >
          I tried this as a test and it worked:
          >
          private void getmember (object o, string member){
          >
          Type type = o.GetType();
          >
          System.Reflecti on.PropertyInfo info =
          type.GetPropert y("Nombre");
          >
          MessageBox.Show (info.GetValue( o,null).ToStrin g());
          >
          }
          I meant 'member' not 'nombre':

          private void getmember (object o, string member){

          Type type = o.GetType();

          System.Reflecti on.PropertyInfo info =
          type.GetPropert y(member);


          MessageBox.Show (info.GetValue( o,null).ToStrin g());

          }

          Comment

          Working...