Updated on Kisan Patel
DLR is created using the classes and interfaces of the
System.Dynamic
namespace. This namespace contains classes, such asExpandoObject
andDynamicObject
. These classes are used to implement runtime features in C#.
The ExpandoObject
class is a class whose members can be explicilty added and removed at run time. The object of this class can be initialized dynamically at runtime.
In other words, the ExpandoObject
class allows dynamic binding of the objects, which enables you to use standard syntax, similar to the dynobj.Method
method instead of using more complex syntax, such as dynobj.getAttribute("Method")
.
The instance of the ExpandoObject
class that is passed as a parameter is treated as dynamic objects in C#. This indicates that the IntelliSense does not work for object members; therefore, the compiler does not raise any errors when non-existent members are called. However an exception occurs at run time when a non-existent member is called in the program.
class Program { public static void Main(string[] args) { //creating variables of dynamic data type dynamic son, father; //initializing an object of ExpandoObject class son = new ExpandoObject(); son.Name = "Son"; son.Age = 22; father = new ExpandoObject(); father.Name = "Father"; father.Age = 45; DynamicWrite(son); DynamicWrite(father); Console.ReadLine(); } public static void DynamicWrite(dynamic member) { Console.WriteLine("{0} is {1} years old.", member.Name, member.Age); } }
the output of the above program…