C # Advanced Programming Chapter 12th Dynamic Language Extensions

Source: Internet
Author: User
(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) =&gt ;        {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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.