Monday, October 12, 2009

.NET 4.0 FAQ Part 1 -- The DLR

Introduction
Where do I get .Net 4.0 from?

What are the important new features in .NET 4.0?

What’s the most important new feature of .NET 4.0?
What is DLR in .NET 4.0 framework?
Can you give more details about DLR subsystem?

How can we consume an object from dynamic language and expose a class to dynamic languages?

Can we see a sample of ‘Dynamic’ objects?

What’s the difference between ‘Dynamic’, ‘Object’ and reflection?

What are the advantages and disadvantage of dynamic keyword?

What are expando objects?

Can we implement our own ‘Expando’ object?

What is the advantage of using custom ‘Expando’ class?

What are IDynamicMetaObjectProvider and DynamicMetaObject?

Can we see performance difference between reflection and dynamic object execution?

Thanks, Thanks and Thanks
.NET 4.0 detail list of new features



Introduction

In this article we will discuss about what new features are provided by .NET framework 4.0. We will then take up the DLR feature and discuss about ‘dynamic’ and ‘expando’ objects. We will also create a custom ‘expando’ class and see what benefits we can get from the same. Many developers mistake ‘dynamic’ objects where made to replace ‘reflection’ and ‘object’ type, we will try to remove this misconception also.
Sorry for the repost. I have deleted the old article due to image upload issues and uploaded again.

Please feel free to download my free 500 question and answer eBook which covers .NET 4.0 , ASP.NET , design patterns, silver light, LINQ, SQL Server , WCF , WPF, WWF@
http://www.questpond.com/

Where do I get .Net 4.0 from?

You can download .NET 4.0 beta from
http://www.microsoft.com/downloads/details.aspx?FamilyID=ee2118cc-51cd-46ad-ab17-af6fff7538c9&displaylang=en

What are the important new features in .NET 4.0?

Rather than walking through the 100 new features list, let’s concentrate on the top 3 features which we think are important. If you are interested to see the detail list of new features click here.
• Windows work flow and WCF 4.0:- This is a major change in 4.0. In WCF they have introduced simplified configuration, discovery, routing service, REST improvements and workflow services. In WWF they have made changes to the core programming model of workflow. Programming model has been made more simple and robust. The biggest thing is the integration between WCF and WWF.
• Dynamic Language runtime: - DLR adds dynamic programming capability to .NET 4.0 CLR. We will talk more about it as this FAQ moves ahead.
• Parallel extensions: - This will help to support parallel computing for multi-core systems. .NET 4.0 has PLINQ in the LINQ engine to support parallel execution. TPL (Task parallel library) is introduced which exposes parallel constructs like parallel ‘For’ and ‘ForEach’ loops, using regular method calls and delegates.
We will be talking in more details of the above features in the coming sections.


What’s the most important new feature of .NET 4.0?

WCF and WWF new features are one of the most interesting features of all. Especially the new programming model of WWF and its integration with WCF will be an interesting thing to watch.
DLR, parallel programming and other new features somehow just seem to be brownie points rather than compelling features. Kathleen Dollard's talks about it in more details
http://msmvps.com/blogs/kathleen/archive/2009/01/07/the-most-important-feature-of-net-4-0.aspx
We will be seeing more of these features as we continue with the FAQ.

What is DLR in .NET 4.0 framework?

DLR (Dynamic language runtime) is set of services which add dynamic programming capability to CLR. DLR makes dynamic languages like LISP, Javascript, PHP,Ruby to run on .NET framework.


There are two types of languages statically typed languages and dynamically typed languages. In statically typed languages you need to specify the object during design time / compile time. Dynamically typed languages can identify the object during runtime. .NET . DLR helps you to host code written in dynamic languages on top of your CLR.


Due to DLR runtime, dynamic languages like ruby, python, JavaScript etc can integrate and run seamlessly with CLR. DLR thus helps to build the best experience for your favorite dynamic language. Your code becomes much cleaner and seamless while integrating with the dynamic languages.


Integration with DLR is not limited to dynamic languages. You can also call MS office components in a much cleaner way by using COM interop binder.
One of the important advantages of DLR is that it provides one central and unified subsystem for dynamic language integration.


Can you give more details about DLR subsystem?

DLR has 3 basic subsystems:-

• Expression trees: - By this we can express language semantics in form of AST (Abstract syntax tree). DLR dynamically generates code using the AST which can be executed by the CLR runtime. An expression tree is a main player which helps to run various dynamic languages javascript, ruby with CLR.
• Call site caching: - When you make method calls to dynamic objects DLR caches information about those method calls. For the other subsequent calls to the method DLR uses the cache history information for fast dispatch.
• Dynamic object interoperability (DOI):- DOI has set of classes which can be used to create dynamic objects. These classes can be used by developers to create classes which can be used in dynamic and static languages.
We will be covering all the above features in more detail in the coming FAQ sections.



How can we consume an object from dynamic language and expose a class to dynamic languages?

To consume a class created in DLR supported dynamic languages we can use the ‘Dynamic’ keyword. For exposing our classes to DLR aware languages we can use the ‘Expando’ class.
So when you want to consume a class constructed in Python , Ruby , Javascript , COM languages etc we need to use the dynamic object to reference the object. If you want your classes to be consumed by dynamic languages you need to create your class by inheriting the ‘Expando’ class. These classes can then be consumed by the dynamic languages. We will be seeing both these classes in the coming section.


Do not forget to download help document for library authors regarding how to enable the dynamic language across platform using DLR
http://dlr.codeplex.com/Wiki/View.aspx?title=Docs

Can we see a sample of ‘Dynamic’ objects?

We had already discussed that ‘Dynamic’ objects helps to consume objects which are created in dynamic languages which support DLR.The dynamic keyword is a part of dynamic object interoperability subsystem.
If you assign an object to a dynamic type variable (dynamic x=new SomeClass()), all method calls, property invocations, and operator invocations on ‘x’ will be delayed till runtime, and the compiler won't perform any type checks for ‘x’ at compile time.
Consider the below code snippet where we are trying to do method calls to excel application using interop services.
// Get the running object of the excel application
object objApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
// Invoke the member dynamically
object x = objApp.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, objApp, null);
// Finally get the value by type casting
MessageBox.Show(x.ToString());

The same code we now write using ‘dynamic’ keyword.
// Get the object using
dynamic objApp1 = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
// Call the
MessageBox.Show(objApp1.Name);

You can clearly notice the simplification of property invocation syntax. The ‘invokemember’ is pretty cryptic and prone to errors. Using ‘dynamic’ keyword we can see how the code is simplified.


If you try to view the properties in VS IDE you will see a message stating that you can only evaluate during runtime.


What’s the difference between ‘Dynamic’, ‘Object’ and reflection?

Many developers think that ‘Dynamic’ objects where introduced to replace to ‘Reflection’ or the ‘Object’ data type. The main goal of ‘Dynamic’ object is to consume objects created in dynamic languages seamlessly in statically typed languages. But due to this feature some of its goals got overlapped with reflection and object data type.
Eventually it will replace reflection and object data types due to simplified code and caching advantages. The main goal of dynamic object was never introduced in the first place to replace ‘reflection’ or object data type, but due to overlapping features it did.


What are the advantages and disadvantage of dynamic keyword?

We all still remember how we talked bad about VB6 (Well I loved the language) variant keyword and we all appreciated how .NET brought in the compile time check feature, well so why are we changing now.
Well, bad developers will write bad code with the best programming language and good developers will fly with the worst programming language. Dynamic keyword is a good tool to reduce complexity and it’s a curse when not used properly.

So advantages of Dynamic keyword:-

• Helps you interop between dynamic languages.
• Eliminates bad reflection code and simplifies code complexity.
• Improves performance with method call caching.
Disadvantages:-

• Will hit performance if used with strongly typed language.

What are expando objects?

‘Expando’ objects serve the other side of interoperability i.e. enabling your custom classes to be consumed in dynamic languages. So you can create an object of ‘Expando’ class and pass to dynamic languages like ruby, javascript, python etc. ‘Expando’ objects helps to add properties on fly. It’s an efficient implementation of dynamic property bag. In order to use ‘ExpandoObject’ we first import ‘System.Dynamic’ namespace.
using System.Dynamic;
We then create the object of ‘ExpandoObject’ and assign it to an object which created from ‘Dynamic’ class type. Please note if we have used ‘Dynamic’ objects and not expand objects as we still do not know what properties are going to be created during runtime.

dynamic obj = new ExpandoObject();

For creating dynamic property we just need to write the property name and set the value.

obj.Customername = "Some Customer Name";

Finally we display the value.
MessageBox.Show(obj.Customername);


Can we implement our own ‘Expando’ object?

‘Expando’ object internally is nothing but properties added to a collection. So you can create your own version of ‘Expando’ object.
The first thing we need to do is inherit from ‘DynamicObject’ class.
public class clsMyExpando : DynamicObject
{
}

As said previously we need to define a collection where we can save properties. In the second step we have created a dictionary object to maintain the properties in the collection.
public class clsMyExpando : DynamicObject
{
Dictionary items= new Dictionary();
}

We can now use the ‘TryGetMember’ and ‘SetGetMember’ to define our get and set properties.
public class clsMyExpando : DynamicObject
{
Dictionary items = new Dictionary();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return items.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
items[binder.Name] = value;
return true;
}}

We can now create object of our customer ‘expando’ class and assign the same to the dynamic class reference. In the below code snippet we have assigned a dynamic property called as ‘Name’.
dynamic obj = new clsMyExpando();
obj.Name = "Dynamic Property";


What is the advantage of using custom ‘Expando’ class?

Custom ‘Expando’ object can be used to gain performance. In case your class has some static properties and some dynamic properties, then you can create static properties in the custom expand class itself as shown in the below code. So when the static properties of the object are called it will not make calls to the dictionary collection thus increasing performance. DLR engine first tries to call the property names rather than calling the ‘TryGetMember’ or ‘TrySetMember’.

First thing avoid ‘expando’ custom classes if you do not have dynamic property requirement and you do not want to communicate with dynamic languages. In case you have dynamic property requirement ensure that properties which you are very sure are added to the class and dynamic properties are implemented by inheriting the dynamicobject class.
public class clsMyExpando : DynamicObject
   {
Dictionary items
       = new Dictionary();
  
       private string _Name;
       public string Name
       {
           get
           {
               return _Name;
           }
           set
           {
               _Name = value;
           }
       }
       public override bool TryGetMember(
           GetMemberBinder binder, out object result)
       {
           return items.TryGetValue(binder.Name, out result);
       }

       public override bool TrySetMember(
           SetMemberBinder binder, object value)
       {
           items[binder.Name] = value;
           return true;
       }

   }


What are IDynamicMetaObjectProvider and DynamicMetaObject?

‘Dynamic’ objects implement ‘IDynamicMetaObjectProvider’ and return ‘DynamicMetaObject’. Both these interfaces are core classes which implement interoperability between dynamic languages. So when should we use it:-

  • If we want to implement our custom logic for fast dynamic property retrieval we can implement ‘IDynamicMetaObjectProvider’ and ‘DynamicMetaObject’. For instance you would like to provide a fast algorithm to retrieve the most used properties. So you can plug-in the algorithm using ‘IDynamicMetaObjectProvider’.


Can we see performance difference between reflection and dynamic object execution?

Coming soon….

Thanks, Thanks and Thanks


http://tomlev2.wordpress.com/category/code-sample/
has sample code which
shows how to implement dynamic objects.

http://dlr.codeplex.com/Wiki/View.aspx?title=Docs%20and%20specs
:- Codeplex
link for the DLR project.

http://msmvps.com/blogs/kathleen/archive/2009/01/07/the-most-important-feature-of-net-4-0.aspx

:- Discusses about the most important feature of .NET 4.0

http://blogs.msdn.com/brada/archive/2008/10/29/net-framework-4-poster.aspx

:- A nice 4.0 complete posture , ensure you have silver light plug-in to take
advantage of the zoom in feature.

http://www.hanselman.com/blog/C4AndTheDynamicKeywordWhirlwindTourAroundNET4AndVisualStudio2010Beta1.aspx

:- Sir Hansel talks about the dynamic keyword.
http://www.codeproject.com/KB/cs/dynamicfun.aspx
:- Dynamic example
implementation.

http://en.wikipedia.org/wiki/Dynamic_Language_Runtime
:- Wiki link for DLR.
http://msdn.microsoft.com/en-us/library/dd233052(VS.100).aspx
:- Download
link for 4.0 and VS 2010 .

http://www.developerfusion.com/article/9576/the-future-of-net-languages/2/

:- Discusses about new language features of 4.0 , I have not seen anything
simpler than this on the web.

http://www.gotnet.biz/Blog/file.axd?file=2009%2F6%2FHow+I+Learned+to+Love+Metaprogramming+-+CodeStock+2009.pdf

:- Nice PDF by Kevin MVP for meta programming.

.NET 4.0 detail list of new features

Thanks to
http://blogs.msdn.com/brada/archive/2008/10/29/net-framework-4-poster.aspx publish a detail poster of .NET 4.0 features.