Home > Articles > Programming > Windows Programming

Building Dynamic Systems with Expressions in .NET

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.
Like this article? We recommend

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020