What is DLR ? what is dynamic in C#?
For better understanding use following piece of code in a C# window application
object a = "1";
int r = 10 + a;
When you compile above code will give compile time error: "Operator '+' cannot be applied to operands of type 'int' and 'object'" because you can not use + operator with two different types except some type like string with int etc.
Now use following code using dynamic
dynamic a = "1";
int r = 10 + a;
It will compile successfully, but this will throw following exception while executing this code "Cannot implicitly convert type 'string' to 'int'"
Because When you will execute code with dynamic type, DLR will dispatch it to CLR as System.String.
If you change above code as given below it will compile and executed successful because "a" will be dispatched as System.32 to CLR by DLR.
dynamic a = 1;
int r = 10 + a;
MessageBox.Show(a.GetType().ToString());// It will show System.Int32
//because type of "a" is dispatched as System.Int32 to CLR via DLR
When you use dynamic type, compiler will bypass compile time type checking for that object. This type and their operation will resolve at run time via DLR.
DLR provides set of services to CLR for supporting dynamic languages.
DLR has basically three services.
Expression trees: DRL uses expression trees to represent language semantics.
Call Site Caching: Dynamic call site caching is a place in code where we are performing operation on/with dynamic object. In our above example dynamic a = 1; int r = 10 + a; and MessageBox.Show(a.GetType().ToString());. DLR stores the charecteristics of dynamic type a(type of objects) and information about operation(like assignment of value type, addition with int etc). If these code has been executed once, these information will be stored in Call Site caching and DLR will fetch these necessary information from cache for fast dispatch to CLR or other run time like silverlite and MS Office.
Dynamic object interoperability: The DLR provides a set of classes and interfaces that represent dynamic objects and operations and can be used by language implementers and authors of dynamic libraries. These classes and interfaces include IDynamicMetaObjectProvider, DynamicMetaObject, DynamicObject, and ExpandoObject.
DLR uses Binder to communicate with call site using expression trees.
dynamic C# Example:
For a simple use case scenario, suppose you are creating one dotnet library which has one Person class.
Person class has three members Name, Address and Age. Now in your another application you want to use it and you want to store age as double and in other application you want to assign age as text. For this what you can do ?
You can declare Age as object but again this will require explicit type casting for further use.
But if you are declaring Age as dynamic then you can use it directly and it will be treated as type of what value you are assigning to it.
You can refer following example.
public class Person
public string Name
public dynamic Age
public string Address
in one application you can use it as
Person person1 = new Person();
person1.Name = "ssp";
person1.Age = 10.5; // It will be treated as System.Double
person1.Address = "Some Address";
//Increase age with 2 years
person1.Age = person1.Age + 2.0;
and in another
Person person2 = new Person();
person2.Name = "ssp";
person2.Age = "10 year and 6 months"; // It will be treated as System.string
person2.Address = "Some Address";