原文:http://ayende.com/blog/1606/reflection-emit-vs-codedom

Both technologies allow you to generate executable code at runtime (even though CodeDOM may not have been originally intended for this). This opens up some interesting possibilities (Rhino Mocks and NHibernate, for instnace, uses Dynamic Proxy, which uses Reflection.Emit to generate proxies for object at runtime).

(这两种技术允许你在运行时生成的可执行代码,这开辟了一些有趣的可能性)

Reflection.Emit gives you complete control of the generated IL, while CodeDOM leaves you creating source code dynamically.(Reflection.Emit让你能完全控制IL生成,而CodeDOM让你动态创建源代码)

Reflection.Emit has the following advantages(Reflection.Emit有以下优点):

  • You can generate new classes directly into an existing assembly, this helps reduce memory consumtion.(你可以生成新类,直接放到现有的程序集中,这有助于减少内存消耗)
  • You can generate new methods in a class (.Net 2.0, dynamic methods).(你可以在类中生成新的方法)
  • Significanty faster than any other alternative.(比其他方式要快)

And disadvantages:(Reflection.Emit的缺点)

  • It is rocket science mixed with the black arts. The common path is merely hard, but try doing something like support out/ref values, or handling sealed methods, and you are thrown into a mire of special cases. That is completely ignoring the new stuff in .Net 2.0, like generics, which are such a fun to try to support.(这是科学与巫术的混合。很难用,但是尝试做一些事情,比如支持out/ref值,或者处理密封的方法。完全忽略了.NET 2中的新东西,比如泛型。)
  • It is not debuggable, at all.(几乎不能调试)
  • It is very easy to produce invalid code, and there isn't much information about what is wrong, nor is there much information about how you should go about it.(容易产生无效代码,而且没有太多的错误信息,也没有太多关于你应该如何处理的信息)
  • Side note: Even the C# Compiler can be presuded to produce invalid code in certain instances, just to give you an idea about how hard this thing is.(注意:即便在某些情况下,可以让C#编译器生产无效代码,但这只是让你了解这件事本身有多难)

CodeDOM has the following advantages:(CodeDOM有以下优点)

  • Very easy to understand and use.(简单易用)
  • The generated code is debuggable.(生产的代码可调试)
  • You get a lot more information about the errors, often with how to fix them.(你会得到更多关于错误的信息,通常是关于如何修复它们的)

And disadvantages:(CodeDOM的缺点)

  • You can't incremently add classes to assembly (important for things like Dynamic Proxy). Every time you generate code, it has its own own assembly.(你不能再现有程序集中添加类,每次生成代码,它都有它自己的程序集)
  • You are limited to what you can do within the langauge. There are things you just can't do in code, which are easily possible in Reflection.Emit. There are usually other ways around it, though.(你只能做开发语言支持的事情,有些事情CodeDOM无法实现,但是可以很容易的使用Reflection.Emit来实现,不过这里通常有其他方式解决)
  • Slower than Reflection.Emit, because we are generating the code and then compiling it on the fly. This is usually very little code, so it doesn't really matter, and caching helps quite a bit.(比Reflection.Emit慢,因为我们在程序运行时生产代码并编译代码。如果是少量代码就没什么性能影响,并且使用缓存会有很大帮助)

Given that caching is going to be used anyway, I think that I can ignore most of the performance benefits of using Reflection.Emit, in favor of a much simpler programming model offered by CodeDOM. The only thing that really needs Reflection.Emit is dynamically adding methods to a class, and that is not something that you often need.

(因为可以使用缓存,所以,我们完全可以忽略Reflection.Emit的性能优势,而倾向于CodeDOM的简单实用。只有当你需要向类中动态添加方法,或者向程序集添加类时,才需要考虑使用Reflection.Emit)

【译】Reflection.Emit vs. CodeDOM的更多相关文章

  1. [读行者][学习LinqExpression和Reflection(Emit)]阅读TypeBuilderSample之ExampleFromTheArticle

    前言 关于”读行者“ 俗语有云:"读万卷书,行万里路“.多读一些优秀代码,不仅可以锻炼我们读代码的能力(便于维护或相互交流),还可以吸取很多我们成长所需的知识点.多读,才能开阔我们的眼界,才 ...

  2. System.Reflection.Emit学习

    C#反射发出System.Reflection.Emit学习 分享: 1 一.System.Reflection.Emit概述 Emit,可以称为发出或者产生.与Emit相关的类基本都存在于Syste ...

  3. System.Reflection.Emit摘记

    动态类型在.net中都是用什么类型来表示的.程序集:System.Reflection.Emit.AssemblyBuilder(定义并表示动态程序集)构造函数:System.Reflection.E ...

  4. C#反射发出System.Reflection.Emit学习

    一.System.Reflection.Emit概述 Emit,可以称为发出或者产生.与Emit相关的类基本都存在于System.Reflection.Emit命名空间下.反射,我们可以取得形如程序集 ...

  5. [EF] - 动态创建模型:System.Reflection.Emit + Code First

    动态创建Entity Framework模型并且创建数据库 使用System.Reflection.Emit+Code First model创建以下的一个实体类和DbContext并且创建数据库: ...

  6. ulua、tolua原理解析

    在聊ulua.tolua之前,我们先来看看Unity热更新相关知识. 什么是热更新 举例来说: 游戏上线后,玩家下载第一个版本(70M左右或者更大),在运营的过程中,如果需要更换UI显示,或者修改游戏 ...

  7. 如何快速编写和调试 Emit 生成 IL 的代码

    .NET Core/.NET Framework 的 System.Reflection.Emit 命名空间为我们提供了动态生成 IL 代码的能力.利用这项能力,我们能够在运行时生成一段代码/一个方法 ...

  8. 再谈使用Emit把Datatable转换为对象集合(List<T>)

    一.前因和存在的问题 前面我写了一篇<使用Emit把Datatable转换为对象集合(List<T>)>的博文,其实起源于我自己编写的一个orm工具(见前面几篇博文有介绍),里 ...

  9. DataTable转Entity(Emit版)

    public static List<T> ToList<T>(DataTable dt)        {            List<T> list = n ...

随机推荐

  1. Spring+SpringMvc+Mybatis 框架的搭建(一)

    本文是因为实习结束后学习到了新的技术,想写下来和更多人交流.开发中遇到的问题我也会一一说明,希望有更多人可以互相探讨,加入到一起来. 1. Spring+SpringMvc +Mybatis 的作用有 ...

  2. 【JavaScript】让事件支持先发布后订阅

    之前写过一个的事件管理器,就是普通的先订阅后发布模式.但实际场景中我们需要做到后订阅的也能收到发布的消息.比如我们关注微信公众号,还是能看到历史消息的.类似于qq离线消息,我先发给你,你登录了就能收到 ...

  3. .net 程序集

    前言:用了几天的时间把高级编程里面程序集一章看完了,原来自己只知道写代码,右键添加引用,从来也不知道操作的实质是什么,微软总是这个套路,鼠标点点就能把任务完成,这对新手友好但是对要通透了解程序执行和内 ...

  4. 【Java SE】如何用Java实现直接选择排序

    摘要:直接选择排序属于选择排序的一种,但是它的排序算法比冒泡排序的速度要快一些,由于它的算法比较简单,所以也比较适合初学者学习掌握. 适宜人群:有一定Java SE基础,明白Java的数据类型,数组的 ...

  5. 记录——excel导出lua工具(python实现)

    项目需要一个从excel导出lua配置表的工具,之前的工具是主程写的,效率极差,i7 CPU 一次全部导出要花掉1个多小时.匪夷所思的是,这么渣的效率,居然用了整整一年.当 然,中途有人反映效率差,主 ...

  6. 案例分享|某医药集团的BI建设案例

    相比于传统型BI,越来越多的企业开始接受并青睐新型的自助式BI,因其项目上线快,失败风险小,简单易用,颇受赞誉.以下是某医药集团上线帆软BI系统FineBI的案例,从用途架构.指标分析.和信息交互几方 ...

  7. PHP 无限级分类(递归)

    网上有很多,这是我自己做测试用的$arr = array( array('id'=>1,'name'=>'电脑','pid'=>0), array('id'=>2,'name' ...

  8. Centos6.5_x86上Oracle11g2 32位的安装与卸载以及相关问题汇总

    需要注意的问题: 1.Linux包括内核和要安装的oralce版本是否符合(这个在官方文档中有说明). 2.安装oracle一般会新建一个为oracle的账户,注意在安装的过程中的root和oracl ...

  9. CI集成phpunit Error: No code coverage driver is available 的解决

    CI集成phpunit时,运行报No code coverage driver is available的错误,如下图: yanglingdeMacBook-Pro:tests yangling$ p ...

  10. DevCloud让代码检查更科学

    代码检查是软件开发工作中不可或缺的一部分,众所周知,规范化的编码是一个优质项目的保证.华为软件开发云(DevCloud)便提供了专业科学的自动化代码检查工作. 一.华为软件开发云(DevCloud)目 ...