Mono.Linq.Expressions update 2
I just tagged the 1.2 version of Mono.Linq.Expressions, and pushed an updated nuget package.
Mono.Linq.Expressions is a utility library to complement the System.Linq.Expressions namespace, and works with .net 4.0 just as fine as it does with Mono. With a bit over 220 downloads of the nuget package, it's short of roughly 160,400 downloads to be the most downloaded nuget package : a stunning success to put it simply.
This post is the first of a short series to detail what's awesome and new in this version.
Extension methods for a fluent construction of expression trees.
Have you been using the expression tree API to build a representation of code at runtime ? If so you're familiar with the Expression class, and it's load of factory methods.
You're also familiar with this kind of code:
var user = Expression.Parameter(typeof (User), "user");
var isFemaleUserOver18 = Expression.Lambda<Func<User, bool>>(
Expression.AndAlso(
Expression.GreaterThanOrEqual(
Expression.Property(user, "Age"),
Expression.Constant(18)),
Expression.Equal(
Expression.Property(user, "Gender"),
Expression.Constant(Gender.Female))), user);
If you take some time to parse this code, the intent is to create an expression tree similar to the one the compiler would emit if you were to write:
Expression<Func<User, bool>> isFemaleUserOver18 =
user => user.Age >= 18 && user.Gender == Gender.Female;
Mono.Linq.Expressions 1.2 contains a code generated series of extension methods to simplify the manual construction of expression trees by fluently chaining the invocations. This allows you to write instead:
var user = typeof (User).Parameter("user");
var isFemaleUserOver18 = Expression.Lambda<Func<User, bool>>(
user.Property("Age").GreaterThanOrEqual(18.Constant())
.AndAlso(
user.Property("Gender").Equal(Gender.Female.Constant())), user);
Not only is the code shorter, but it's also easier on the eyes, and easier to comprehend. Using this, almost the factory methods calls to the Expression class can be written fluently.
TechDays 2011

Mercredi, j’aurai l’opportunité de présenter Mono et son écosystème pendant les TechDays Microsoft.
Ce sera l’occasion de faire le point sur les dernières nouveautés de Mono, et de montrer comment réutiliser ses compétences .net et partager son code C# pour cibler des plateformes en vogue, comme l’iPhone et l’iPad avec MonoTouch, Android avec MonoDroid et Mac OS X avec MonoMac.
Si vous ne pouvez pas assister à la session, je serai aussi disponible sur le stand alt.net pour discuter de tous ces sujets. Venez nombreux !
Custom Linq Expressions
Between .net 3.5 and .net 4.0, the Linq Expression Tree API changed quite a bit, going from a simple compiler supporting expressions only, to a full fledged compiler supporting expressions as well as statements, on top of which are implemented all DLR based language. For anyone working closely with this API, the Expression Tree spec is a great source of information, the MSDN isn’t very helpful there.
When the spec comes to describing the object model for nodes, it differentiates several kind of nodes:

The core nodes and the common nodes are those which are implemented inside System.Linq.Expressions, and common nodes are implemented on top of the core nodes, using a process that is called reducing nodes.
A reducible node is simply a node that can be compiled as a compound of core nodes. We’re not going to talk about library specific extensions, which are nodes that you would write if you were to implement, say, a LINQ provider. We’ll focus on reducible nodes.
I took some time last year to implement a few helper nodes that are not available in .net 4.0, but, according to the spec, may be included in a future release. They’re mapped to similar C# constructs, and are pretty useful it you generate complex code using expression trees. Here’s the list of custom expressions I added in Mono.Linq.Expressions, my project to complement the System.Linq.Expression namespace:
Even if those map to a C# statement, they’re suffixed with `Expression` for consistency with the whole System.Linq.Expressions namespace. Using them is as simple as, heh, adding a using directive:
using Mono.Linq.Expressions;
And using the factory methods on the CustomExpression class to create those new nodes:
var d = Expression.Parameter (typeof (DisposableType), "d");
var printAndDispose = Expression.Lambda<Action<DisposableType>> (
CustomExpression.Using (
d,
Expression.Call (typeof (Console).GetMethod (
"WriteLine", new [] { typeof (object) }), d)),
d);
The Expression Tree API makes it easy to write custom reducible expressions, all you have to do, is to inherit from Expression, override CanReduce and NodeType, and implement the Reduce method:
public abstract partial class CustomExpression : Expression {
public override ExpressionType NodeType {
get { return ExpressionType.Extension; }
}
public override bool CanReduce {
get { return true; }
}
/* ... */
}
If we take, for instance, our UsingExpression, mapping to the C# using statement, Reduce is implemented as follows:
public override Expression Reduce ()
{
var end_finally = Expression.Label ("end_finally");
return Expression.Block (
new [] { variable },
Expression.Assign (variable, disposable),
Expression.TryFinally (
body,
Expression.Block (
Expression.Condition (
Expression.NotEqual (variable, Expression.Constant (null)),
Expression.Block (
Expression.Call (
Expression.Convert (variable, typeof (IDisposable)),
typeof (IDisposable).GetMethod ("Dispose")),
Expression.Goto (end_finally)),
Expression.Goto (end_finally)),
Expression.Label (end_finally))));
}
Just like a C# using, it will try to execute a block, and whether the block triggered an exception or not, it will call IDisposable.Dispose on the subject of the using statement.
Wasn’t that easy? Even if those nodes are pretty general, writing custom and specific nodes is a powerful and simple way to factorize code when generating code using the Expression Tree API.
In the end, I added in Mono.Linq.Expressions a CustomExpression type, our base class for well, custom expressions. We provide a CustomExpressionVisitor which extends the standard ExpressionVisitor to support our custom expressions, and the CSharpWriter has been updated to support them as well:
[Test]
public void Using ()
{
var arg = Expression.Parameter (typeof (IDisposable), "arg");
var lambda = Expression.Lambda<Action<IDisposable>> (
CustomExpression.Using (
arg,
Expression.Call (typeof (Console).GetMethod (
"WriteLine", new [] { typeof (object) }), arg)),
arg);
AssertExpression (@"
void (IDisposable arg)
{
using (arg)
{
Console.WriteLine(arg);
}
}
", lambda);
}
Next feature for Mono.Linq.Expressions: using Mono.Reflection to turn a delegate into an expression of this delegate.
IQueryable support for WP7 3
Or how to use APIs missing from the WP7 SDK.
Monday saw the official launch of Windows Phone 7, at this occasion, I downloaded the SDK to see how it compares to MonoTouch and MonoDroid. Basically, it’s based on the Compact Framework Base Class Library, on top of which is exposed the Silverlight UI API.
The Compact Framework has historically targeted devices with limited capabilities. The Compact Framework BCL is a trimmed down version of the vanilla .net BCL. One notable missing feature is runtime code generation through System.Reflection.Emit. As such, you can’t use DynamicMethods, nor can you compile assemblies on the device, and nor can you compile Expression Trees. You can expect to find missing tidbits sprinkled throughout the whole BCL. But luckily for us, there’s Mono.
The Mono BCL being licensed under the MIT/X11, you can easily take C# code from our libraries and use it inside your own applications. And that whenever our code is not tied to a runtime feature. This means that you couldn’t bring System.Reflection.Emit to WP7, but that you could probably bring XmlDocument with a bit of work.
Basically, this reminded me of what db4o did to bring support for Expression Trees to the Compact Framework using code from Mono. I figured I’d try to do the same for WP7. If you played with the WP7 SDK, you probably know that you have access to Expression Trees, and the compiler can emit them without any issue. But LambdaExpression and Expression of T don’t have their Compile method you’re used to. And WP7 is missing IQueryable and folks.
So as an example, I extracted our Expression Tree interpreter, which is used, guess what, when you can’t compile Expression Trees at runtime, and our IQueryable support. Here we are, by just referencing this System.Linq.dll, you can write code such as:
using System.Linq;
var data = new [] { 1, 2, 3, 4, 5, 6, 7, 8 };
var query = data.AsQueryable ();
query.Where (i => i % 2 == 0);
int count = query.Count ();
Or:
using System.Linq.Expressions;
var p1 = Expression.Parameter (typeof (string), "x");
var p2 = Expression.Parameter (typeof (string), "y");
Expression<Func<string, Func<string, Func<string>>>> e =
Expression.Lambda<Func<string, Func<string, Func<string>>>> (
Expression.Lambda<Func<string, Func<string>>> (
Expression.Lambda<Func<string>> (
Expression.Call (
typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
new [] { p1, p2 }),
new ParameterExpression [0]),
new [] { p2 }),
new [] { p1 });
var f = e.Compile ();
var f2 = f ("Hello ");
var f3 = f2 ("World !");
var result = f3 ();
(Crazy, I know)
You can simply take the assembly and reference it from your projects, or compile yourself the code. Let me know if this has been useful to you.
To conclude, I’d say don’t hesitate to take code from Mono to fill the voids in the WP7 SDK. Who knows if you won’t end up contributing missing parts or filing bugs afterwards.
