C#編譯器對於dynamic對象到底做了什麼

來源:互聯網
上載者:User

標籤:style   blog   color   os   ar   div   log   sp   line   

private static void TestMethod(){            //dynamic 僅僅是個預留位置而已            dynamic p1 = new { X = 1, Y = 89 };            //對dynamic對象p1的X屬性訪問通過CallSite來實現,並且CallSite可以Cache            object p1x = p1.X;            object p1y = p1.Y;            //dynamic 僅僅是個預留位置而已            dynamic p2 = new { X = 1, Y = 89, Z = 3 };            object p2x = p2.X;            object p2y = p2.Y;            object p2z = p2.Z;}

對於如上所示的含有dynamic調用的C#方法編譯後大致的結果如下:

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#編譯器對於dynamic對象到底做了什麼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.