C # What does the compiler do to the dynamic object?

Source: Internet
Author: User
Private Static void testmethod () {// dynamic is just a placeholder. Dynamic p1 = new {x = 1, y = 89 }; // access to the X attribute of Dynamic Object P1 is implemented through callsite, and callsite can cache object p1x = p1.x; object p1y = p1.y; // dynamic is just a placeholder. Dynamic P2 = new {x = 1, y = 89, Z = 3}; object P2x = p2.x; object p2y = p2.y; object p2z = p2.z ;}

The following figure shows the general result of compiling C # methods with dynamic calls:

using System;using Microsoft.CSharp.RuntimeBinder;using System.Runtime.CompilerServices;private static void TestMethod(){    object p1 = new {        X = 1,        Y = 0x59    };        if (TestMethod_SiteContainer.p_Sitea == null)    {        TestMethod_SiteContainer.p_Sitea = CallSite<Func<CallSite, object, object>>.Create(Binder.GetMember(CSharpBinderFlags.None, "X", typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));    }    object p1x = TestMethod_SiteContainer.p_Sitea.Target(TestMethod_SiteContainer.p_Sitea, p1);        if (TestMethod_SiteContainer.p_Siteb == null)    {        TestMethod_SiteContainer.p_Siteb = CallSite<Func<CallSite, object, object>>.Create(Binder.GetMember(CSharpBinderFlags.None, "Y", typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));    }    object p1y = TestMethod_SiteContainer.p_Siteb.Target(TestMethod_SiteContainer.p_Siteb, p1);        object p2 = new {        X = 1,        Y = 0x59,        Z = 3    };        if (TestMethod_SiteContainer.p_Sitec == null)    {        TestMethod_SiteContainer.p_Sitec = CallSite<Func<CallSite, object, object>>.Create(Binder.GetMember(CSharpBinderFlags.None, "X", typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));    }    object p2x = TestMethod_SiteContainer.p_Sitec.Target(TestMethod_SiteContainer.p_Sitec, p2);        if (TestMethod_SiteContainer.p_Sited == null)    {        TestMethod_SiteContainer.p_Sited = CallSite<Func<CallSite, object, object>>.Create(Binder.GetMember(CSharpBinderFlags.None, "Y", typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));    }    object p2y = TestMethod_SiteContainer.p_Sited.Target(TestMethod_SiteContainer.p_Sited, p2);        if (TestMethod_SiteContainer.p_Sitee == null)    {        TestMethod_SiteContainer.p_Sitee = CallSite<Func<CallSite, object, object>>.Create(Binder.GetMember(CSharpBinderFlags.None, "Z", typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));    }    object p2z = TestMethod_SiteContainer.p_Sitee.Target(TestMethod_SiteContainer.p_Sitee, p2);}[CompilerGenerated]private static class TestMethod_SiteContainer{    // Fields    public static CallSite<Func<CallSite, object, object>> p_Sitea;    public static CallSite<Func<CallSite, object, object>> p_Siteb;    public static CallSite<Func<CallSite, object, object>> p_Sitec;    public static CallSite<Func<CallSite, object, object>> p_Sited;    public static CallSite<Func<CallSite, object, object>> p_Sitee;}

 

C # What does the compiler do to the dynamic object?

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.