(i) DLR
the dynamic capabilities of C#4 are part of the dynamic Language runtime ,theDLR . the DLR is a series of services added to the CLR.
(ii) type of dynamic
The dynamic type allows you to write code that ignores type checking during compilation. The compiler assumes that Any action defined for an object of type dynamic is valid and the compiler does not detect an error before running.
Example:
Dynamic person = "human"; string firstName = man. FirstName;
These two lines of code can be compiled by the compiler, but the click-to-run error will be:
It is important to note that Dynamic type is useful, but it comes at a price.
(iii) including DLR scriptruntime
Adding script editing to the application and passing values to the script and outgoing values from the script are the applications that can do the work with the script.
(iv) DynamicObject and ExpandoObject
from You can create your own dynamic objects by deriving from DynamicObject or by using ExpandoObject.
Use DynamicObject derivation creates a dynamic object that requires overriding 3 methods Trysetmembe (), Trygetmember (), and Tryinvokemember ().
Use The difference between ExpandoObject and DynamicObject is that there is no overriding method.
Example:
Class program{static void Main (string[] args) {func<string, string, string> Getfullname = (f, l) => ; {return F + "" + l;}; Dynamic byexobj = new ExpandoObject (); Byexobj. FirstName = "Li"; Byexobj. LastName = "Four"; Byexobj. Getfullname = Getfullname; Console.WriteLine (Byexobj. GetType ()); Console.WriteLine (Byexobj. Getfullname (byexobj. FirstName, Byexobj. LastName)); Console.WriteLine ("====================="); Dynamic dyobj = new Mydynamicobject (); Dyobj. FirstName = "Zhang"; Dyobj. LastName = "three"; Dyobj. Getfullname = Getfullname; Console.WriteLine (Dyobj. GetType ()); Console.WriteLine (Dyobj. Getfullname (dyobj. FirstName, Dyobj. LastName)); Console.readkey (); }}public class mydynamicobject:dynamicobject{dictionary<string, object> dynamicdata = new Dictionary<string , object> (); public override bool Trysetmember (SetMemberBinder Binder, object value) {Dynamicdata[binder. Name] = value; return true; } public override bool Trygetmember (GetMemberBinder binder, out object result) {bool success = false; result = NULL; if (Dynamicdata.containskey (binder). Name) {result = Dynamicdata[binder. Name]; Success = true; } else {result = "The value of this property was not found"; Success = false; } return success; } public override bool Tryinvokemember (Invokememberbinder binder, object[] args, out object result) {dynamic method = Dynamicdata[binder. Name]; Result = Method ((string) args[0], (String) args[1]); return result! = NULL; }}
Run the above code with the following results: