Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get/Set object properties using 'System.Reflection'

Get/Set object properties using 'System.Reflection'

Scheduled Pinned Locked Moved C#
tutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lea Hayes
    wrote on last edited by
    #1

    Hi all! I have written a method which allows me to access nested properties within objects. The function which I have written returns a PropertyDescriptor, the property component, and property value when given an object and property string. So for example, if I had an object called "person" it would be possible to access a particular property like:

    object component = null, value = null;

    PropertyDescriptor desc = person.FindPropertyDescriptor("Occupation.Address.Road", ref component, ref value);

    // In effect this is the equivalent of:
    object value = person.Occupation.Address.Road;

    Here is the function which I have written:

        /// <summary>
        /// Finds property descriptor of object from a property name string.
        /// </summary>
        /// <param name="obj">Object for which to search.</param>
        /// <param name="propertyName">Property, i.e. "Size.Width".</param>
        /// <param name="nestedObject">Outputs nested component.</param>
        /// <param name="nestedValue">Outputs component value.</param>
        /// <returns>Property descriptor.</returns>
        public static PropertyDescriptor FindPropertyDescriptor(this object obj, string propertyName, ref object nestedComponent, ref object nestedValue)
        {
            // Find required property.
            object activeObject = null, nextObject = obj;
    
            PropertyDescriptor propDesc = null;
            string propNamePart = "";
            int lastIndex = 0, nextIndex = 0;
    
            while (nextIndex != -1)
            {
                // Initialize active object.
                activeObject = nextObject;
    
                // Fetch next property name substring.
                nextIndex = propertyName.IndexOf('.', lastIndex);
                if (nextIndex != -1)
                {
                    propNamePart = propertyName.Substring(lastIndex, nextIndex - lastIndex);
                    lastIndex = nextIndex + 1;
                }
                else
                    propNamePart = propertyName.Substring(lastIndex, propertyName.Length - lastIndex);
    
                // Fetch nested property.
                PropertyDescriptorCollection activeProperties = TypeDescriptor.GetProperties(activeObject);
                foreach (PropertyDescriptor pDesc in activeProperties)
                {
                    if (pDesc.DisplayName == propNamePart)
    
    D 1 Reply Last reply
    0
    • L Lea Hayes

      Hi all! I have written a method which allows me to access nested properties within objects. The function which I have written returns a PropertyDescriptor, the property component, and property value when given an object and property string. So for example, if I had an object called "person" it would be possible to access a particular property like:

      object component = null, value = null;

      PropertyDescriptor desc = person.FindPropertyDescriptor("Occupation.Address.Road", ref component, ref value);

      // In effect this is the equivalent of:
      object value = person.Occupation.Address.Road;

      Here is the function which I have written:

          /// <summary>
          /// Finds property descriptor of object from a property name string.
          /// </summary>
          /// <param name="obj">Object for which to search.</param>
          /// <param name="propertyName">Property, i.e. "Size.Width".</param>
          /// <param name="nestedObject">Outputs nested component.</param>
          /// <param name="nestedValue">Outputs component value.</param>
          /// <returns>Property descriptor.</returns>
          public static PropertyDescriptor FindPropertyDescriptor(this object obj, string propertyName, ref object nestedComponent, ref object nestedValue)
          {
              // Find required property.
              object activeObject = null, nextObject = obj;
      
              PropertyDescriptor propDesc = null;
              string propNamePart = "";
              int lastIndex = 0, nextIndex = 0;
      
              while (nextIndex != -1)
              {
                  // Initialize active object.
                  activeObject = nextObject;
      
                  // Fetch next property name substring.
                  nextIndex = propertyName.IndexOf('.', lastIndex);
                  if (nextIndex != -1)
                  {
                      propNamePart = propertyName.Substring(lastIndex, nextIndex - lastIndex);
                      lastIndex = nextIndex + 1;
                  }
                  else
                      propNamePart = propertyName.Substring(lastIndex, propertyName.Length - lastIndex);
      
                  // Fetch nested property.
                  PropertyDescriptorCollection activeProperties = TypeDescriptor.GetProperties(activeObject);
                  foreach (PropertyDescriptor pDesc in activeProperties)
                  {
                      if (pDesc.DisplayName == propNamePart)
      
      D Offline
      D Offline
      Derek Bartram
      wrote on last edited by
      #2

      Personally I always make any dynamically loaded object conform to a set interface, then you don't need to find the various properties of the object, but simple instantiate from the dynamic loader and cast. Makes life a lot easier. I'm wondering if you are trying to reference code not specific to your application, in which case you would have to do it your way. http://www.codeproject.com/KB/library/dynamicloading.aspx[^] (The way I do it).

      L 1 Reply Last reply
      0
      • D Derek Bartram

        Personally I always make any dynamically loaded object conform to a set interface, then you don't need to find the various properties of the object, but simple instantiate from the dynamic loader and cast. Makes life a lot easier. I'm wondering if you are trying to reference code not specific to your application, in which case you would have to do it your way. http://www.codeproject.com/KB/library/dynamicloading.aspx[^] (The way I do it).

        L Offline
        L Offline
        Lea Hayes
        wrote on last edited by
        #3

        Hi, I require this functionality for my undo/redo system. When an object property is changed via a property grid control, a property action is created. At the moment I am storing the property component along with the previous and new values. At various stages my program replaces some of these components, and so when the undo/redo action is later executed, the action is not actually undone to the current object. So instead I have done a trial which generates a property name string from the object by enumerating through the parent items of the changed property grid item. Then later this same string can be used to access the property again (using the method I posted previously). I have read through the article which you have very kindly posted, however I do not think I can apply it for this particular application because some of the properties exist within the .NET runtime, and others are either of a dynamic variety, or exist within my C# code. Thank you for your advise, it is greatly appreciated. Lea Hayes

        D 1 Reply Last reply
        0
        • L Lea Hayes

          Hi, I require this functionality for my undo/redo system. When an object property is changed via a property grid control, a property action is created. At the moment I am storing the property component along with the previous and new values. At various stages my program replaces some of these components, and so when the undo/redo action is later executed, the action is not actually undone to the current object. So instead I have done a trial which generates a property name string from the object by enumerating through the parent items of the changed property grid item. Then later this same string can be used to access the property again (using the method I posted previously). I have read through the article which you have very kindly posted, however I do not think I can apply it for this particular application because some of the properties exist within the .NET runtime, and others are either of a dynamic variety, or exist within my C# code. Thank you for your advise, it is greatly appreciated. Lea Hayes

          D Offline
          D Offline
          Derek Bartram
          wrote on last edited by
          #4

          Crikey sounds complicated! Good luck.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups