C# Advanced Programming Chapter 12 Dynamic Language Extension

大家讲道理
Release: 2017-04-11 14:05:59
Original
2214 people have browsed it

(1) The dynamic function of DLR

C#4 is Dynamic Language Runtime(Dynamic language runtime, part of DLR). DLR is a set of services added to CLR.

(2) dynamic type

dynamic type allowed Write code that ignores type checking during compilation. The compiler assumes that any operation defined for a object of type dynamic is valid, and the compiler does not check for errors before running it.

Example:


dynamic person = "人";string firstName = person.FirstName;
Copy after login

These two lines of code can be compiled by the compiler, but after clicking run, an error will be reported:

It should be noted that although the dynamic type is very useful, it comes at a cost.

(3) Contains DLR ScriptRuntime

Add scripts to the application Editing functions, and passing values ​​to and from scripts, allow applications to use scripts to complete work.

(4) DynamicObject and ExpandoObject

from You can create your own dynamic objects by deriving DynamicObject or using ExpandoObject.

Using DynamicObject derivation to create dynamic objects requires overriding three methods: TrySetMembe(), TryGetMember() and TryInvokeMember().

The difference between using ExpandoObject and DynamicObject for derivation is that there is no need to override the 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 = "李";
        byexobj.LastName = "四";
        byexobj.GetFullName = getFullName;
        Console.WriteLine(byexobj.GetType());
        Console.WriteLine(byexobj.GetFullName(byexobj.FirstName, byexobj.LastName));
        Console.WriteLine("=====================");
        dynamic dyobj = new MyDynamicObject();
        dyobj.FirstName = "张";
        dyobj.LastName = "三";
        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 = "未找到该属性的值";
            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;
    }
}
Copy after login

Run the above code, the results are as follows:

The above is the detailed content of C# Advanced Programming Chapter 12 Dynamic Language Extension. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!