Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

DLR : Expressions and a ‘Hello World’ application

0.00/5 (No votes)
23 Oct 2011 1  
DLR Expression is the backbone of the DLR. It is a separate feature that you can use without involving the rest of the DLR.Let’s take a look at

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

DLR Expression is the backbone of the DLR. It is a separate feature that you can use without involving the rest of the DLR.

Let’s take a look at what DLR Expression is first, before getting into the examples. DLR Expression is much like a programming language. It has constructs like the loop expressions, assignment expressions, and method invocation expressions you normally see in other languages. For example, a Hello World program in C# looks like this:

Console.Writeline(“Hello World”);

The equivalent code in DLR Expression looks like this:

MethodInfo Console_WriteLine_MethodInfo =
typeof(Console).GetMethod(“WriteLine”,
new Type[] { typeof(string) });

Expression CallExpression =
Expression.Call(null,
Console_WriteLine_MethodInfo,
Expression.Constant(“Hello World”));

Action CallDelegate =
Expression.Lambda<Action>(CallExpression).Compile();

CallDelegate();

So what are the differences between DLR Expression and a normal programming language, other than that the code in DLR Expression looks a ton more verbose? There are three key differences:

  • Code as data and data as code—code expressed in DLR Expression is data that can be more easily analyzed and worked on.
  • A common denominator of multiple languages—Like CLR’s IL instructions, DLR expressions serve as the common denominator of multiple languages.
  • No concrete syntax, only abstract syntax—DLR Expression defines only abstract syntax and no concrete syntax. However, it supports serialization and we can use that to serialize abstract syntax to concrete syntax or, conversely, to deserialize concrete syntax into abstract syntax.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here