Tenera IT blog
  • About

Category Archives: Reflection

Get property values by name

Posted on October 12, 2013 by PeterVD Posted in Reflection

Here are some extension methods to get the values of properties, supporting navigation of properties. For example:

  • customer.GetValueForPath(“Name”)
  • order.GetValueForPath(“Customer.Name”)

This might come in handy once in a while. (I used it to export the data displayed in a Datagrid)

  1. ///<summary>
  2. /// Gets the value for path.
  3. ///</summary>
  4. ///<param name=”obj”>The object to get the value from.</param>
  5. ///<param name=”path”>The path of the property to get (Path of property names).</param>
  6. public static object GetValueForPath(this object obj, string path)
  7. {
  8.     if (obj == null) return null;
  9.     var type = obj.GetType();
  10.     var propertyPath = type.GetPropertyPath(path);
  11.     return propertyPath == null ? null : obj.GetValueForPath(propertyPath);
  12. }
  13. ///<summary>
  14. /// Gets the value for path.
  15. ///</summary>
  16. ///<param name=”obj”>The object to get the value from.</param>
  17. ///<param name=”path”>The path of the property to get (Path of PropertyInfo’s).</param>
  18. ///<returns></returns>
  19. public static object GetValueForPath(this object obj, IEnumerable<PropertyInfo> path)
  20. {
  21.     object result = obj;
  22.     foreach (var info in path)
  23.     {
  24.         if (result == null) break;
  25.         result = info.GetValue(result, null);
  26.     }
  27.     return result;
  28. }
  29. ///<summary>
  30. /// Translates a string property path, to a list of PropertyInfo objects
  31. ///</summary>
  32. ///<param name=”type”>The type of the base object.</param>
  33. ///<param name=”path”>The path to get the PropertyInfo’s for.</param>
  34. ///<returns></returns>
  35. public static IList<PropertyInfo> GetPropertyPath(this Type type, string path)
  36. {
  37.     var propertyPath = new List<PropertyInfo>();
  38.     if (!string.IsNullOrEmpty(path))
  39.     {
  40.         var propertyNames = path.Split(‘.’);
  41.         var currentType = type;
  42.         foreach (var propertyName in propertyNames)
  43.         {
  44.             var prop = currentType.GetProperty(propertyName);
  45.             if (prop == null)
  46.             {
  47.                 Debug.WriteLine(“TypeExtensions.GetPropertyPath Error: Property ‘{0}‘ does not exist on type ‘{1}‘. In path ‘{2}‘ on type ‘{3}‘”, propertyName, currentType.Name, path, type.Name);
  48.                 break;
  49.             }
  50.             propertyPath.Add(prop);
  51.             currentType = prop.PropertyType;
  52.         }
  53.     }
  54.     return propertyPath;
  55. }
Navigational properties Reflection

Add attributes to a property at runtime.

Posted on June 22, 2011 by PeterVD Posted in Reflection, WinForms Leave a comment

In a project I had to create a little test application to create and edit objects of a referenced assembly. This was very early in the development stage, so the classes could possibly change a lot during the course of the project.

Instead of designing a UI for each class and getting into the process of changing it over and over again, I decided to use Reflection and the PropertyGrid control in winForms.

Continue reading →

Attributes PropertyGrid Reflection WinForms

Tag cloud

Architecture Attributes Barcode Design Framework Navigational properties OO Printing PropertyGrid Reflection Remoting Serialization Silverlight WCF WinForms Work WPF

Categories

  • Architecture (3)
  • Reflection (2)
  • Silverlight (1)
  • Uncategorized (1)
  • WCF (1)
  • WinForms (1)
  • WPF (2)

Pages

  • About

Archives

  • October 2013
  • April 2013
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • June 2011

Categories

  • Architecture (3)
  • Reflection (2)
  • Silverlight (1)
  • Uncategorized (1)
  • WCF (1)
  • WinForms (1)
  • WPF (2)

WordPress

  • Log in
  • WordPress

CyberChimps WordPress Themes

© Tenera it blog