Tenera IT blog
  • About

Monthly Archives: October 2013

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

Showing and printing Barcodes in WPF

Posted on October 11, 2013 by PeterVD Posted in WPF

Recently I had to render barcodes in a WPF client and had to be able to print them as well.

On this project we were using DevExpress controls, but the Barcode control (at least till version 13.1) was only available for reporting. There was no ‘regular’ WPF control to display barcodes on screen.

I first tried using a Barcode font. I found the website of John T Barton with lots of info about barcodes. Downloaded the latest version of the EAN128 font from the website of Grand Zebu, but unfortunately I ran into problems with the first barcodes I tried (“MSN12345” and “MSN00000”). One of the characters created by the code from the site (to calculate checksum and some compression) wasn’t supported by the font.

After some extra research on the internet I quickly came across the Zen Barcode Rendering framework on Codeplex. This free framework looked like the answer to my problems/requirements.

Unfortunately, it targeted ASP.NET, SSRS and WinForms, but no WPF. So …. no luck there. But, as the framework is an open source project, I downloaded the code and started investigating if it would be possible to make some modifications so it could be used by WPF clients. Continue reading →

Barcode Printing WPF

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