Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of dynamic language extension in C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about a sample analysis of dynamic language extensions in C #. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

(1) DLR

The dynamic capabilities of Category 4 are part of Dynamic Language Runtime (dynamic language Runtime, DLR). DLR is a series of services added to CLR.

(2) dynamic type

Dynamic types allow you to write code that ignores type checking during compilation. The compiler assumes that any operation defined on an object of type dynamic is valid and that the compiler does not detect errors before running.

Example:

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

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

It is important to note that while the dynamic type is useful, it comes at a cost.

(3) including DLR ScriptRuntime

Add script editing function to the application, and pass values to and from the script, so that the application can use the script to complete the work.

(IV) DynamicObject and ExpandoObject

Derive from DynamicObject or use ExpandoObject to create your own dynamic objects.

To create dynamic objects using DynamicObject derivation, you need to override three methods, TrySetMembe (), TryGetMember (), and TryInvokeMember ().

The difference between deriving with ExpandoObject and DynamicObject is that there is no need to override the method.

Example:

Class Program {static void Main (string [] args) {Func getFullName = (f, l) = > {return f + "" + 1;}; 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 dynamicData = new Dictionary () 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 = "value not found for this attribute"; 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, and the result is as follows:

Thank you for reading! This is the end of this article on "sample Analysis of dynamic language extensions in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report