zoukankan      html  css  js  c++  java
  • C#编译器对于dynamic对象到底做了什么

    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;
    }
  • 相关阅读:
    PL/SQL连接错误:ora-12705:cannot access NLS data files or invalid environment specified
    java 后台开发关键词解释
    架构、框架和设计模式关系(转)
    Web.xml配置详解
    SVN“验证位置时发生错误”的解决办法
    java遍历给定目录,树形结构输出所有文件,包括子目录中的文件
    android EditText 默认情况下不获取焦点(不弹出输入框)
    android listview item取消按点击效果
    如何实现可动态调整隐藏header的listview
    eclipse 启动到loading workbench... 自动关闭
  • 原文地址:https://www.cnblogs.com/zanxiaofeng/p/3939438.html
Copyright © 2011-2022 走看看