One of the new features, a.k.a. syntactic sugars, in C# 3.0 is extension methods. At first, it looks like compiler magic and it is no big deal. But isn’t it the same about class method? When a function argument is hidden, suddenly whole world changes. Clearly, OOP is not just a hidden this pointer, but we can think of extension methods as class methods that will only access the public fields. This may look like a step backwards but we are not living in an ideal world, in fact not even close to ideal. This is more like a side-step that will provide new ways forward. One of these new ways is LINQ, language integrated query. However, it is hard to come up with another as useful as LINQ. Is there a way to utilize extension methods in WPF as we did lambda expressions?
While introducing the lambda converters, I said that implementing IValueConverter or IMultiValueConverter is not a complete waste of time. There were two reasons behind this statement and hence two problems ahead of lambda converter with dreams of being the ultimate binding converter.
One of them was code maintainability in large projects, where a converter can be used more than once. Typing the same expression again and again is not an option.
Idea occurred to me, while reading Kent Bogaart‘s article on WPF converters and Josh Smith‘s article on piping value converters. They have both suggested a way to reuse and combine converters. Then, I had one of those, wait a minute, moments. This is where extension methods kick in. We could write our Convert method in an extension method and use them inside the expressions. Not only, maintaining these converters will be easier, but also they will be combinable by nature.
Again, we had to make a few changes to the Dynamic Expression API. Looking for extension methods will be a last resort in a function call. We have the type of the instance, but unfortunately extension methods won’t show up in the type’s methods. These methods are defined in a seperate static class, but they are instance methods. Is there a contradiction here? No, static keyword’s original meaning is that the following function’s address will be constant. It does not mean that function belongs to a class rather than an instance. Here, static denotes that all elements inside the class will be static methods. Extension method calls does not provide the these extension types, so we have to find out all of the static classes before making a decision.
You can skip this paragraph if you are not interested in the details. If you take a look at these classes by reflection, you will notice that they are nothing but abstract sealed classes. At the first call to our new GetExtensionMethod method, we will query these kind of types and put their suitable public static methods in to a lookup table. These methods must have a return value and at least one parameter. If two extension methods with same name and suitable parameters are ambiguous according to the current implementation. Implementation is not hard but I had to take a look into the standards. Please, feel free to ask for an implementation if you ever need one, or better you could provide your suggestion if you are not lazy as I am right now. Thanks to LINQ, this addition took less than half an hour. I am thinking of a series dedicated to LINQ. Again, I surrounded the necessary changes to dynamic.cs by WPFix regions.
The other one was the ConvertBack method, which had no counter-part in a lambda converter. Now that lambda converters are suitable for multiple uses, the need for a backward conversion is higher.
We will simply provide a second expression as a parameter to our markup extension class, LambdaExtension. I was going to simply use another constructor with two parameters, but I guess things are different now that the VS 2008 is released. We have to use the name/value syntax to support backward conversion.
I’m still not sure about multibinding and backward conversion. I did write the required changes, however I didn’t even test it once. I will if I have such a need. Can you point out such a case?
Although it is not production quality code, you can download WPFix3 Solution (VS 2008) and take a look. Don’t you wish your RTM had something like hot this? Now, if only intelli-sense was extensible enough.
With this post, I am putting a break on WPFix series. In the future, I will continue the series, with hopefully something different than binding and converting. Until then, I will continue updating codes according to your feed-backs.