【译】Reflection.Emit vs. CodeDOM

时间:2024-09-10 17:35:20

原文: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)