65.9K
CodeProject is changing. Read more.
Home

DLR : Expressions and a ‘Hello World’ application

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Oct 11, 2013

CPOL

1 min read

viewsIcon

6483

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

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.