Mono.Linq.Expressions
Mono.Linq.Expressions is an helper library to complement the System.Linq.Expressions namespace.
It works on both Mono >= 2.8 and .net >= 4.0.
API
public static class CSharp {
public string ToCSharpCode (Expression) {}
}
Returns a string containing the C# representation of the expression.
public static class FluentExtensions {}
Provides extension methods to ease the creation of expression trees. For instance, instead of writing:
var field = Expression.Field (
Expression.Convert (parameter, typeof (string)),
"Length");
You can write:
var field = parameter.Convert (typeof (string)).Field ("Length");
public abstract class CustomExpression {
public abstract Expression Accept (CustomExpressionVisitor visitor) {}
}
Base class for custom expressions. Accept takes a custom visitor which extends the built-in ExpressionVisitor with support for custom expressions.
public static class CombineExtensions {
public static Expression<T> Combine<T> (
this Expression<T> self,
Func<Expression, Expression> combinator) where T : Delegate {}
public static Expression<T> Combine<T> (
this Expression<T> self,
Expression<T> expression,
Func<Expression, Expression, Expression> combinator) where T : Delegate {}
}
Helper to combine a fully created lambda with another into a brand new lambda. This is done by rewriting and inlining the bodies in the resulting lamba. With this, combining two predicates with a And expression or negating an expression can be simply written:
public static Expression<Func<T, bool>> AndAlso<T> (
this Expression<Func<T, bool>> self,
Expression<Func<T, bool>> expression)
{
return self.Combine (expression, Expression.AndAlso);
}
public static Expression<Func<T, bool>> Not<T> (
this Expression<Func<T, bool>> self)
{
return self.Combine (Expression.Not);
}
public abstract class ExpressionWriter {}
Provides a base class for pretty print specific language, such as CSharpWriter used by CSharp.ToCSharpCode().
public class DoWhileExpression : CustomExpression {}
A
do {} while (condition);
statement.
public class ForEachExpression : CustomExpression {}
A
foreach (var item in iterable) {}
statement.
public class ForExpression : CustomExpression {}
A
for (initializer; condition; increment) {}
statement.
public class UsingExpression : CustomExpression {}
A
using (disposable) {}
statement.
public class WhileExpression : CustomExpression {}
A
while (condition) {}
statement.
public static class PredicateBuilder {
public static Expression<Func<T, bool>> OrElse<T> (
this Expression<Func<T, bool>> self,
Expression<Func<T, bool>> expression) {}
public static Expression<Func<T, bool>> AndAlso<T> (
this Expression<Func<T, bool>> self,
Expression<Func<T, bool>> expression) {}
public static Expression<Func<T, bool>> Not<T> (
this Expression<Func<T, bool>> self) {}
}
Provides a way to combine lambda predicates using boolean operators. Expressions are rewritten to keep the predicates simple and understandable by LINQ providers. For instance:
Expression<Func<User, bool>> isOver18 = u => u.Age > 18;
Expression<Func<User, bool>> isFemale = u => u.Gender == Gender.Female;
Expression<Func<User, bool>> femalesOver18 = isOver18.AndAlso (isFemale);
// >> femalesOver18.ToString ()
// u => u.Age > 18 && u.Gender == Gender.Female