Home > Articles > Programming > Windows Programming

Building Dynamic Systems with Expressions in .NET

  • Print
  • + Share This
  • 💬 Discuss
From the author of
The Expression API and the capabilities of the DLR lower the barriers to entry for creating systems where code and algorithms are created at runtime. Bill Wagner, author of Effective C#: 50 Specific Ways to Improve Your C#, Second Edition, shows how to use the Expression API and dynamic runtime support to build dynamic systems based on the data in your application.

Introduction

The two most recent versions of C# and .NET make creating dynamic systems easier. The addition of the Dynamic Language Runtime (DLR) and more extensive Expression API support lower the barrier to creating code as data and then working with that code at runtime. Previously you needed to use Reflection.Emit or other low-level APIs, but now you have a better toolset. The Expression API isn't simple, however; you still need to do quite a bit of work in order to create code at runtime.

Expression trees are useful in two instances:

  • When creating code at runtime, the Expression API is simpler than using Reflection.Emit.
  • When you want to use the symbolic information in code, parsing expression trees by using the Expression API is easier than using the Reflection APIs to parse code and objects.

In this article, I'll discuss how the Expression API works, showing some of the features it enables. You'll see that programming with expressions resembles programming with Func types, as you already do in LINQ. The difference is that expressions enable you to examine the code in a symbolic way and make use of those symbols.

Parsing Expressions

Let's begin by parsing expressions. I recently worked on a project where I had to transmit objects using the MetaWeblog API. I'm a fan of LINQ, so I used the LINQ to XML APIs to create the XML representation of the objects I needed to transmit. It's not difficult to write, but it is somewhat cumbersome. For example, here's the method that creates an XElement for the getCategories API:

public static XElement CreateRequest(string blog, string user,
    string password)
{
    return new XElement("methodCall",
        new XElement("methodName", "metaWeblog.getCategories"),
        new XElement("params",
            new XElement("param",
                new XElement("value",
                    new XElement("string", blog)
                )
            ),
            new XElement("param",
                new XElement("value", user)
            ),
            new XElement("param",
                new XElement("value",
                    new XElement("string", password)
                )
            )
        )
    );
}

The getCategories API is one of the simpler APIs in the MetaWeblog protocol. The method that creates a Post is more than twice that long. I really wanted to create these XML documents with less extra code and ceremony. Using the Expression API gave me a way to create structs and classes that would write the XML element for me. Writing XElements for the getCategories API is a simple matter of programming, using techniques you already know. However, what I want to do is create a struct that writes elements based on the names of the properties in those struct s. For example, this is the XML representation of a Post object for the newPost or editPost API:


  
    categories
    
      
        
          misc
        
      
    
  
  
    description
    
      This is the body of the post.
    
  
  
    title
    This is a sample post
  
  
    dateCreated
    
      20040716T19:20:30
    
  

Sure, I could write code that handles this task—and every other struct or class type. But it's more reusable to write a method that uses the Expression API to generate the XElements for each individual node in the struct. Then, all I'll need to do is create a type that represents a Post object and let it create XML for itself.

The code we need is a method that writes an XElement using a given code symbol as the name, and the result of an expression as the value. This design gives me a great deal of flexibility, enabling me to write methods and properties that return the value I want and simplify the XML creation. Furthermore, the design provides resilience in the serialized XML. If you want to change the name of an XML node, just refactor the name of the property or method call. Because the internal code parses the expression tree to create the XElement names, changing the method or property name will automatically change the XElement name. I'll start with the Post class, which shows the usage for this API and gives you a feeling for how the API looks:

public class Post
{
    private List categoryStorage = new List();

    public string title { get; set; }
    public string description { get; set; }
    public DateTime dateCreated { get; set; }
    public IEnumerable categories
    {
        get { return categoryStorage; }
    }

    public void AddCategories(params string[] categories)
    {
        categoryStorage.AddRange(categories);
    }

    public XElement toXml()
    {
        return new XElement("struct",
            this.ToXmlMember(() => title),
            this.ToXmlMember(() => dateCreated),
            this.ToXmlMember(() => description),
            this.ToXmlMember(() => categories));
    }
}

The Post class has the four members shown above in the sample XML, contains methods to add categories, and includes one other method to create the XML representation.

The toXml() method uses the ToXmlMember() extension method to generate an XElement representation of that particular property. Notice that the parameter to this method is not the value of the property, but rather a lambda expression that returns the property. This approach enables you to avoid all the strings in the code and generate XML elements with the proper names.

Let's dive inside the extension methods to see how to leverage the expression trees to create the XML elements.

public static class XElementGeneratorExtensions
{
    public static XElement ToXElement(this object item,
        Expression> property)
    {
        var value = property.Compile()();
        string name = getMemberName(property);
        return new XElement(name, value.ToString());
    }

    public static XElement ToXmlMember(this object item,
        Expression> property)
    {
        return new XElement("member",
            new XElement("name", getMemberName(property)),
            new XElement("value", property.Compile()().ToString())
        );
    }

    public static XElement ToXmlMember(this object item,
        Expression> property)
    {
        return new XElement("member",
            new XElement("name", getMemberName(property)),
            new XElement("value",
                new XElement("dateTime.iso8601",
                    property.Compile()().ToString())
                )
        );
    }

    public static XElement ToXmlMember(this object item,
        Expression>> property)
    {

        return new XElement("member",
            new XElement("name", getMemberName(property)),
                new XElement("value",
                    new XElement("array",
                        from val in property.Compile()()
                        select new XElement("data",
                            new XElement("value", val.ToString()
                        )
                    )
                )
            )
        );
    }

    private static string getMemberName(Expression> property)
    {
        var body = property.Body as
            System.Linq.Expressions.MemberExpression;
        var func = property.Body as
            System.Linq.Expressions.MethodCallExpression;
        string name = "unknown";
        if (body != null) // It is a property, or a field, or an indexer
            name = body.Member.Name;
        else if (func != null) // It's a method call
            name = func.Method.Name;
        return name;
    }
}

This class shows several of the techniques you'll use when you parse and use expression trees. Let's begin by looking at the method signatures. Notice that instead of Func, the lambda expressions are typed as Expression>. That design changes the parameter from a delegate to a data structure that represents your code. That data structure enables you to examine the code semantically and take action based on the symbols in the expression.

In this sample, I do two things with the expression. First I compile the expression and execute it to get the value. Then I call ToString() to convert that value to a string representation for use in the XML nodes:

var value = property.Compile()();

The other task is to examine the expression and determine the name of the property being accessed. That's in the private method getMemberName. Lambda expressions have a body property that returns everything to the right of the lambda operator (=>).getMemberName assumes that the body of the lambda expression is a property, a field, an indexer, or a method call.

In an expression tree, every node is an object derived from System.Linq.Expressions.Expression. The type of object tells you the type of expression. In addition to the MemberExpression and MethodCallExpression, other types include operators (binary, with unary operators derived from operator), new expressions, lambda expressions, parameters, constants, try expressions, blocks, loops, and so on. In short, almost anything that can be expressed in most programming languages can be expressed in an expression tree.

Returning to the task at hand, I limited support to the MethodCallExpression and MemberExpression. The MemberExpression contains a MemberInfo object that describes the member being accessed. From that object, you can easily extract the name of the member. The MethodCallExpression contains a MethodInfo member that contains the name of the method (whether it's an instance or a static method). Once you've extracted the name of the method or property, you can easily create the XML elements you want.

That's all there is to it. This page of code can turn any property or method call into an XML representation by using the name of the class member and its value.

This sample touched on some of possibilities enabled by using expression trees in your code. You can examine the semantic symbols that are part of your program and create algorithms that work with those symbols. In this sample, I was only interested in the name of the method or member being accessed. Creating an IQueryProvider entails examining the body of the expressions to translate those expressions into the form used by the data source. For example, LINQ to SQL translates query expressions from expression trees to SQL statements. (The C# compiler translates C# source code into the expression tree.)

You probably can imagine many other algorithms that are enabled by processing and understanding the symbols in lambda expressions that are part of your code. In all those cases, you can examine the expression tree to understand the structure and the symbols, and then take action on them. It's work, and you should do it only when you gain a lot by doing that extra work. But when you have a very common use case, the Expression API is a powerful tool. By learning how to parse expression trees, you open many new capabilities to solve different programming problems. You'll use "magic strings" much less often, and rely much more on the symbols already embedded in your code. That technique alone will make much of your code more robust in the face of changes.

Creating Expression Trees

Of course, parsing expression trees is only half the story. The Expression API also provides a way to create your own expression trees at runtime. The main difficulty with creating expression trees is that all Expression objects are immutable. Therefore, building expressions involves creating Expression objects and building the final expression from all its smaller components. Also, because different kinds of expressions can appear in many locations in your final expression, creating expressions will often involve casts and conversions. That fact increases the chance for error, and extensive runtime checking will be needed when you create expressions.

To give you a taste of what building expressions entails, here's how you would build the Expression> that would return the title of a Post, and then use that expression to create an XElement for the title:

ConstantExpression target = Expression.Constant(post);
MemberInfo memberInfo = post.GetType().GetMember("title").First();

MemberExpression e = Expression.MakeMemberAccess(target, memberInfo);
LambdaExpression lambda = Expression.Lambda(e, null);
Expression> l = lambda as Expression>;

var xE = post.ToXElement(l);

Let's look at the example line by line. The first line creates the constant expression for the target object (post):

ConstantExpression target = Expression.Constant(post);

The next line retrieves the MemberInfo for the "title" property:

MemberInfo memberInfo = post.GetType().GetMember("title").First();

Next, you need to compose the object and the member info into a MemberExpression. Remember that when we parsed the expression, MemberExpression was the kind of expression we needed to parse a property accessor. Here, we're building the same:

MemberExpression e = Expression.MakeMemberAccess(target, memberInfo);

Once you have the member expression, you need to create the lambda expression that uses the MemberExpression as its body. This lambda expression doesn't take any parameters, so the second parameter is null:

LambdaExpression lambda = Expression.Lambda(e, null);

Finally, you can cast the lambda expression to the particular expression signature that you've built:

Expression> l = lambda as Expression>;

Building expressions often involves similar code. You'll be building Expression objects for each element in a compound expression, and then building compound expressions from those simple expressions. The more complicated the expression, the more complicated the code. Building expressions in code is a somewhat tedious process. In practice, I parse expressions to understand symbols far more often than I create expressions in code. However, the process of building expressions will help you to understand the expression tree model, and you'll be better able to parse expressions when you want that functionality.

Expressions in Everyday Development

The Expression API is quite specialized; it's definitely not a tool you'll use every day. However, when you want to build algorithms that leverage the symbols in your code, you should consider whether the expression tree APIs can make that process easier. In special situations, you may be able to leverage the Expression API to create code at runtime. This option may enable you to create dynamic algorithms based on the data in your program. Expressions are an advanced topic, but well worth the investment of time and effort.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus