Tenera IT blog
  • About

Author Archives: PeterVD

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

Grouchy Brainy Smurf colleague

Posted on April 26, 2013 by PeterVD Posted in Uncategorized

As a consultant you get to work in lots of different places and environments, and with lots of different colleagues.

Most of the persons you meet and have to work with are pretty normal.

But once in a while you get to meet one that is not. 🙂

 

So, with some client I had the doubtful privilege to meet a guy that everybody warned me about beforehand.

Continue reading →

Work

Person Oriented Programming (POP)

Posted on April 30, 2012 by PeterVD Posted in Architecture Leave a comment

With this post I’d like to introduce a new concept in programming:
The person oriented programming (POP) or Polite Object Oriented Programming (POOP).

I haven’t decided on the name yet, although the last acronym doesn’t sound quite as good, I guess. 🙂

Anyway, what is it all about?

The main idea is to treat all your classes, not as a dumb object that you can treat as you like, but as a person you ask things to do for you. Continue reading →

Architecture OO

An application development framework is like Playmobil

Posted on March 5, 2012 by PeterVD Posted in Architecture Leave a comment

Why is it that in almost every IT company, or even every IT project, the inevitable question pops up “whether we should write our own application development framework (ADF) or not?”

Even when there probably are already more ADF attempts in the world than successful IT projects!

So let’s look at it more closely.
What are the driving forces behind the decision to go for a home made ADF?
Continue reading →

Architecture Design Framework OO

Code Readability vs Soc/SRP

Posted on February 21, 2012 by PeterVD Posted in Architecture Leave a comment

This is a blog post with my personal vision on the subject of SoC (Separation of Concerns) and SRP (the Single Responsibility Principle).

First of all, I’m convinced that it’s very important to adhere to these principles as much as possible.
It can really make your code easier to read and maintain. Clean simple classes with one responsibility are usually much easier to understand and to change. A change in some part of the program, will only affect the class that is responsible for that part of the logic.
Read the excellent book “Head First Design Patterns” by Eric and Elisabeth Freeman, if you have no idea of what I mean.

On the other hand, it is my believe that a dogmatic approach can also make your project a mess. I’ve already seen such projects, where even the architect himself found it nearly impossible to find his way in the massive amount of classes.

(Yes, that logic is definitely in this class. No wait, it’s in another implementation of that same interface here, I think.
Now where is it? Oh right, it’s in one of the other projects. Hmm, let’s see. Which one could it be? I think here… hmmm….no…or here…)

This led me to the creation of following chart. Continue reading →

Architecture Design OO

BooleanToVisibilityConverter

Posted on January 31, 2012 by PeterVD Posted in Silverlight, WPF Leave a comment

Just a small piece of code which might come in handy in a Silverlight or WPF project: The typical boolean to System.Visibility converter.
Very often in one of your views, there is a piece of the view that must be shown or hidden in response to the value of a checkbox, for instance.

That’s where this converter comes in.
Continue reading →

Silverlight WPF

WCF Remoting

Posted on June 27, 2011 by PeterVD Posted in WCF Leave a comment

Sometimes you get requirements that really push you to do things the way they’re not supposed to be done.

In a recent project we had to do quite complex operations on the client (WPF) and send complex objects (all operations as one transaction) back to the server, but still the communication to the server had to be done using WCF. This forced us to send objects over the wire that could not be serialized by the default WCF serializer. Continue reading →

Remoting Serialization WCF

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