Get/Set object properties using 'System.Reflection'
-
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)
-
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)
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).
-
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).
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
-
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
Crikey sounds complicated! Good luck.