C#编译器对于dynamic对象到底做了什么
private static void TestMethod()
{
//dynamic 仅仅是个占位符而已
dynamic p1 = new { X = , Y = };
//对dynamic对象p1的X属性访问通过CallSite来实现,并且CallSite可以Cache
object p1x = p1.X;
object p1y = p1.Y; //dynamic 仅仅是个占位符而已
dynamic p2 = new { X = , Y = , Z = };
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 = ,
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 = ,
Y = 0x59,
Z =
}; 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对象到底做了什么的更多相关文章
- AFNetworking到底做了什么
写在开头: 作为一个iOS开发,也许你不知道NSUrlRequest.不知道NSUrlConnection.也不知道NSURLSession...(说不下去了...怎么会什么都不知道...)但是你一定 ...
- volatile关键字到底做了什么?
话不多说,直接贴代码 class Singleton { private static volatile Singleton instance; private Singleton(){} //双重判 ...
- AFNetworking到底做了什么?(二)
接着上一篇的内容往下讲,如果没看过上一篇内容可以点这: AFNetworking到底做了什么? 之前我们讲到NSUrlSession代理这一块: 代理8: /* task完成之后的回调,成功和失败 ...
- 一个Java对象到底占用多大内存?
最近在读<深入理解Java虚拟机>,对Java对象的内存布局有了进一步的认识,于是脑子里自然而然就有一个很普通的问题,就是一个Java对象到底占用多大内存? 在网上搜到了一篇博客讲的非常好 ...
- 一个Java对象到底占用多大内存
在网上搜到了一篇博客讲的非常好,里面提供的这个类也非常实用: import java.lang.instrument.Instrumentation; import java.lang.reflect ...
- 一个Java对象到底占多大内存
最近在读<深入理解Java虚拟机>,对Java对象的内存布局有了进一步的认识,于是脑子里自然而然就有一个很普通的问题,就是一个Java对象到底占用多大内存? 在网上搜到了一篇博客讲的非常好 ...
- new到底做了什么?
下面是一个实例化自定义的对象,我们将要对他进行分析 //定义构造函数 function A(){ this.b = 1 //在这个对象里增加一个属性 //不可以拥有返回对象的return语句 } va ...
- 一个Java对象到底占多大内存?(转)
最近在读<深入理解Java虚拟机>,对Java对象的内存布局有了进一步的认识,于是脑子里自然而然就有一个很普通的问题,就是一个Java对象到底占用多大内存? 在网上搜到了一篇博客讲的非常好 ...
- MySQL实战 | 01-当执行一条 select 语句时,MySQL 到底做了啥?
原文链接:当执行一条 select 语句时,MySQL 到底做了啥? 也许,你也跟我一样,在遇到数据库问题时,总时茫然失措,想重启解决问题,又怕导致数据丢失,更怕重启失败,影响业务. 就算重启成功了, ...
随机推荐
- 编译inotify报错
错误如下: configure: error: no acceptable C compiler found in $PATH 这是因为没有找到编译器的原因造成的 解决方法: 安装gcc [root@ ...
- js广告浮动
一个广告框在指定区域,有定位属性的父级区域内,一直向右向左移动,如果碰到左右边框,反向,如果碰到上下边距,反向,实现在指定框中浮动的效果. <!doctype html> <html ...
- order by跟group by 跟having(2)
- Unity3D 之3D动画机设置
新建一个动画机 然后创建一些动画的属性 每根线都是一个动画到下一个动画的转变,动画的转变是基于条件的. 1.通过建立Parameters设定动画的转换条件 2.右边的Conditions设定可以设定是 ...
- C# 操作数据库的几种方式(数据库使用SQL SERVER2008)
一:通过常规 T-SQL 语句 (只写删除操作,其他同理) string strConn = ConfigurationManager.ConnectionStrings["SiteConn ...
- NUnit使用详解(一)
转载:http://hi.baidu.com/grayworm/item/38e246165aa7b9433a176e90 NUnit是一个单元测试框架,专门针对于.NET来写的,它是是xUnit的一 ...
- git: command not found
在使用git时,出现“git: command not found”的情况,于是使用yum安装: [root@localhost ~]# yum install -y gitLoaded plugin ...
- C# 时间与时间戳互转
/// <summary> /// 将c# DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="t ...
- php 加载函数 __autoload(), spl_autoload_register()
来自:http://www.cnblogs.com/myluke/archive/2011/06/25/2090119.html spl_autoload_register (PHP 5 >= ...
- 对React的理解
转自:http://www.cocoachina.com/webapp/20150721/12692.html 现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了R ...