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?