C# and Expression Trees example

I was just taking a look to the powerful Expression Trees framework in the .NET Framework and I decided to create an example. The result: something that I will never use and I don’t recommend you, but in some way it’s illustrative.

It’s very common to use a class with properties to access the configuration. In this way, you benefit from the static check at compile time, assuming you are in a static language like C#. You can avoid the use of the same string in many places of the code and instead, you use a property like this:

public class AppConf
{
  public static string SomeKey
  {
    get { return ConfigurationManager.AppSettings["SomeKey"]; }
  }
}

Console.WriteLine("AppConf.SomeKey :- {0}", AppConf.SomeKey);

But still, there is a string, and while the string can be moved to a constant what if we want to avoid it? One possible solution is:

public class ExpAppConf
{
  public static string SomeKey
  {
    get
    {
      return ConfigurationManager.AppSettings[
        (new Func<Expression<Func<ExpAppConf, object>>, string>(
          delegate(Expression<Func<ExpAppConf, object>> exp)
          {
            return ((MemberExpression)exp.Body).Member.Name;
          })).Invoke(x => ExpAppConf.SomeKey)];
    }
  }
}

Console.WriteLine("ExpAppConf.SomeKey :- {0}", ExpAppConf.SomeKey);

I’m using Expression Trees, Lambda Expressions and Anonymous Methods, but don’t worry, if you understand all the “trivial-tree paragraph” MVVM blog post out there, you will be fine here.

Which do you think are the possible drawbacks?

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
This entry was posted in Programming. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

6 Comments

  1. Posted April 20, 2010 at 12:12 am | Permalink

    “Which do you think are the possible drawbacks?”

    A headache for whoever has to maintain that.

    • Kieran Benton
      Posted April 20, 2010 at 5:13 am | Permalink

      Amen. Think you’re trying to solve a problem that doesn’t really exist? In reality the string references only usually exist in a single class anyway.

  2. levy
    Posted April 20, 2010 at 2:59 am | Permalink

    Lots of Irritating Superfluous Parentheses

    despite the fact that it’s not lisp

  3. Posted April 20, 2010 at 6:36 am | Permalink

    Cool to know about this C# feature I didn’t know about but, really, how’s that better than using constant strings? This adds quite a lot of complexity for no real benefit.

    • aldenml
      Posted April 20, 2010 at 7:05 am | Permalink

      Hi @Andrea, in this particular case, the only advantage I can see is in the automatic refactor case, since all the references are static type checked. One example of a good application for expression trees is in the Fluent NHibernate library IMO.

  4. Posted April 20, 2010 at 8:05 am | Permalink

    @Gubatron, I agree with you. Seems that similar task in Haskell or Lisp is more legible than to be written in C#. So, I prefer to use C# on the classical way, as imperative descendant language. Also C++0x lambda expressions are more clear to read, so a C++0x lambda expression like:

    [](int x) -> int { return x * x; };

    Is more legible than the C# one, and expression tree is really a headache…

One Trackback

  1. By uberVU - social comments on April 20, 2010 at 3:08 am

    Social comments and analytics for this post…

    This post was mentioned on Twitter by aldenml: @gubatron C#, Expression Trees, Lambda Expressions and Anonymous Methods http://bit.ly/aABbJb...

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • What am I running?

    WordPress trunk, rev. 15157
    Nginx 0.8.36
    PHP 5.3.2-1ubuntu4
    APC 3.1.3p1
    WP Super Cache 0.9.9
    Ubuntu 10.04 LTS
  • Meta