Have you ever wished that you could bind properties to your WPF controls without using string literals to identify property names? This article describes how to use the T4 (Text Template Transformation Toolkit), which is built into Visual Studio 2008, and the Visual Studio automation object model API, to generate member and type information for an entire project. Generated metadata can then be applied to such things as dispensing with string literals in XAML binding expressions and overcoming the INotifyPropertyChanged property name string code smell, or indeed any place you need to refer to a property, method, or field by its string name. There is also experimental support for obfuscation, so member names can be retrieved correctly even after obfuscation. I’ve also ported the template to VB.NET, so our VB friends can join in on the action too.
Introduction
I’ve been spending a lot of time lately on my latest series of Calcium articles (I am in the process of porting Calcium to Silverlight), but this week I confess that I got a little sidetracked. I have discovered the joys of generating code with T4, and I have used it to build a metadata generator for your Silverlight and Desktop CLR projects (C# and VB.NET). It can be used as a replacement for static reflection (expression trees), reflection (walking the stack), and various other means for deriving the name of a property, method, or field. It can also be used, as I will demonstrate, to replace those nasty string literals in XAML path binding expressions, with statically typed properties; allowing the detection of erroneous binding expressions at compile time rather than at runtime.
Read more: Codeproject


