Click here to Skip to main content
15,881,938 members
Articles / General Programming
Article

DLR : Expressions and a ‘Hello World’ application

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 6.1K   3  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --