Wednesday, May 9, 2012


Writing code dynamically can be very useful in some scenarios. In a recent WF project I needed to build a workflow in code and save it to XAML, executing this saved workflow definition latter on. But there's a problem: Lambda expressions do not serialize to XAML. This is where the ExpressionServices "utility" comes in. Using the "Person" type for this example:

(click to enlarge)

Just two properties. Now, keeping in mind the above statement about Lambda Expressions, when generating the Workflow we need to use a method from the ExpressionServices type to convert the Lambda Expressions to Operator Activities:

(click to enlarge)

Finally, lets generate the XAML using XamlServices:

(click to enlarge)

That's it!
Final note: According to MSDN, "this conversion is supported for a limited set of lambda expressions only". I couldn't, however, find a reference to what set of Lambda Expressions are supported.

Edit: Yes, I usually don't place comments on the code I share here. That's because it would make the code less obvious. It's a simple demo.