I’ve been playing around with F# recently, the functional language that shipped with Visual Studio 2010. I’m looking at using it to write an application using WPF and the Model-View-ViewModel architecture. One big requirement is for DataBinding.
When you bind the View to the ViewModel, you typically have to use the explicit name of the property on the ViewModel that you’re binding to (like “Text”). You also need the literal name of the property when you fire off (or receive) the PropertyChanged event. That’s always been a little ugly, because using the literal string means it isn’t compile-time checked. I got around it in C# using a helper class which uses reflection and lambda expressions to look at a piece of code (e.g. o => o.MyProperty) and get the name of the property as a string.
That utility class didn’t work in F#, mostly because F# lambda expressions aren’t the same base object as C# lambda expressions. I was faced with rewriting it. This is where F# seems to shine. Here’s the same “get property name” logic written in F#:
open Microsoft.FSharp.Quotations.Patterns let propertyName quotation = match quotation with | PropertyGet (_,propertyInfo,_) -> propertyInfo.Name | _ -> ""
Here’s how you can use it:
type myClass(p) = member x.MyProperty with get() = p let myObject = new myClass(1) let myPropertyName = propertyName <@ myObject.MyProperty @>
At the end, myPropertyName has been assigned the string value “MyProperty”. It’s a heck of a lot less code. In this case it only works if you have an existing object to run it against. However, you can modify the propertyName function to make it recursively dig through the Lambda and find the PropertyGet:
let rec propertyName quotation = match quotation with | PropertyGet (_,propertyInfo,_) -> propertyInfo.Name | Lambda (_,expr) -> propertyName expr | _ -> "" let myPropertyName = propertyName <@ fun (x : myClass) -> x.MyProperty @>
Now you don’t need to have an instance of the class lying around to get the property name.