Thursday, September 13, 2012

What is Dynamic Type Or define DLR?


Design Time/Compile Time Object:      In .net  programming we can access all the properties and functions at compile time only as illustrated in below example where we are accessing the method of Calculator class(calc.Add(10,20)).
e.g.
Calculator calc = GetCalculator();
Int sum = calc.Add (10, 20);
Run Time Object /Late Binding: In this Case compiler doesn’t checks for compile time error everything is handled at run time. The most important thing is that we can’t access the properties and methods while writing code.

If you wanted to do the exact same thing as in e.g. (like if it were some other class, maybe old-COM interop, or something where the compiler didn't know a priori that Add() was available, etc) you'd do this:
e.g.
object calc = GetCalculator ();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 });
Int sum = Convert.ToInt32 (res);


Before .net 4.0 we are handling any COM Component (dynamically typed languages) as mentioned in e.g. .



We store the COM objects in object Type or in short with the help of reflection and Object Type we till now manage our code while dealing with dynamically typed languages.


DYNAMIC TYPES OR DLR:
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.
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.
E.g. We can write the same code as mentioned in e.g. as below with dynamic Type.


dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);

Please Note here GetCalculator is a COM component method not of the .Net class.

Please follow below links for detail.
http://www.codeproject.com/Articles/42997/NET-4-0-FAQ-Part-1-The-DLR
http://www.hanselman.com/blog/C4AndTheDynamicKeywordWhirlwindTourAroundNET4AndVisualStudio2010Beta1.aspx

 

No comments: