[Reading Notes] C # advanced programming Chapter 1 Dynamic Language extension,

Source: Internet
Author: User

[Reading Notes] C # advanced programming Chapter 1 Dynamic Language extension,

(1) DLR

The Dynamic Function of C #4 is part of the Dynamic Language Runtime (DLR. DLR is a series of services added to CLR.

 

 

(2) dynamic type

Dynamic type allows you to write code that ignores type checks during compilation. The compiler assumes that any operation defined for a dynamic object is valid and will not detect errors before running.

Example:

Dynamic person = "person"; string firstName = person. FirstName;

The two lines of code can be compiled by the compiler, but an error will be reported after you click "run:

 

It should be noted that although the dynamic type is very useful, it has a price.

 

 

(3) including DLR ScriptRuntime

The script editing function is added to the application, and the numeric value is input to the script and the numeric value is output from the script. The application can use the script to complete the work.

 

 

(4) DynamicObject and ExpandoObject

You can derive from DynamicObject or use ExpandoObject to create your own dynamic object.

To create a dynamic object using DynamicObject, you must override three methods: TrySetMembe (), TryGetMember (), and TryInvokeMember ().

The difference between using ExpandoObject and DynamicObject is that you do not need to override the method.

Example:

1 class Program 2 {3 static void Main (string [] args) 4 {5 Func <string, string, string> getFullName = (f, l) ==>{ return f + "" + l ;}; 6 dynamic byexobj = new ExpandoObject (); 7 byexobj. firstName = "Li"; 8 byexobj. lastName = "4"; 9 byexobj. getFullName = getFullName; 10 Console. writeLine (byexobj. getType (); 11 Console. writeLine (byexobj. getFullName (byexobj. firstName, byexobj. lastName); 12 Console. wri TeLine ("=================="); 13 dynamic dyobj = new MyDynamicObject (); 14 dyobj. firstName = "Zhang"; 15 dyobj. lastName = "3"; 16 dyobj. getFullName = getFullName; 17 Console. writeLine (dyobj. getType (); 18 Console. writeLine (dyobj. getFullName (dyobj. firstName, dyobj. lastName); 19 Console. readKey (); 20} 21} 22 23 public class MyDynamicObject: DynamicObject24 {25 Dictionary <string, object> dynamicData = new D Ictionary <string, object> (); 26 public override bool TrySetMember (SetMemberBinder binder, object value) 27 {28 dynamicData [binder. name] = value; 29 return true; 30} 31 public override bool TryGetMember (GetMemberBinder binder, out object result) 32 {33 bool success = false; 34 result = null; 35 if (dynamicData. containsKey (binder. name) 36 {37 result = dynamicData [binder. name]; 38 success = true; 39} 40 else4 1 {42 result = "the value of this attribute is not found"; 43 success = false; 44} 45 return success; 46} 47 public override bool TryInvokeMember (InvokeMemberBinder binder, object [] args, out object result) 48 {49 dynamic method = dynamicData [binder. name]; 50 result = method (string) args [0], (string) args [1]); 51 return result! = Null; 52} 53}

Run the above Code and the result is as follows:

 

 

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.