为什么编译后代码注入比预编译代码注入更好?
所以我们都知道 C# 没有类似 C 的宏预处理器(并且有一个很好的线程解释为什么 此处)。但现在 AOP 越来越受欢迎,似乎我们开始使用后处理器做一些我们过去使用预处理器做的事情(请记住,我只是接触了 PostSharp 所以我可能没有根据)。
我非常喜欢 C# 中的属性,但如果出于充分的原因而排除了预处理器(作为前 MFC 用户,我仍然对此表示怀疑,但仍然接受),为什么编译后代码注入比编译前代码注入更好?编译代码注入?
So we all know that C# doesn't have a C-like macro pre-processor (and there's a good thread on why here). But now that AOP is gaining traction, it seems like we're starting to do stuff with post-processors that we used to do with pre-processors (bear in mind that I am only getting my feet wet with PostSharp so am perhaps off base).
I am a huge fan of attributes in C#, but if a pre-processor was left out for good reasons (which, as a former MFC user I still question but nevertheless accept) why is post-compilation code injection a better idea than pre-compilation code injection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
5年前设计PostSharp时选择后编译的原因是:
也就是说,C/C++ 的 AOP 实现确实是一个预编译器 (WeaveC),而 Java 中的实现是一个编译器扩展(因为 Java 编译器有许多 OSS 实现)。
-盖尔
The reasons why I chose post-compilation when designing PostSharp 5 years ago are:
That said, implementations of AOP for C/C++ are indeed a pre-compiler (WeaveC) and implementations in Java are a compiler extension (for the good reason that there are many OSS implementations of the Java compiler).
-gael
从技术上讲,Visual Studio 中内置了 C# 的预编译选项: 文本模板转换工具包(T4)。这允许您在预编译步骤中完成非常令人惊奇的事情,并且是很多产品的基础,例如一些 ORM 等。
Technically, there is a pre-compilation option for C# built into Visual Studio: The Text Template Transformation Toolkit (T4). This allows you to do pretty amazing things in a pre-compilation step, and is the basis of quite a few products, such as some ORMs, etc.
如果您要进行预编译,则必须解释您支持的所有不同语言的源文件,然后在将其传递给编译器之前以该语言生成代码。通过后处理,您可以简单地使用反射来检查程序集,无论原始语言是 C#、Visual Basic 还是其他语言。
If you were to do pre-compilation you would have to interpret the source files from all the different languages you support then generate code in that language before it gets passed to the compiler. With post-processing you can simply use reflection to examine the assemblies whether the original language was C#, Visual Basic, or whatever.
只是更简单而已。 IL 比 C# 源代码更容易解析。它与语言无关。
It is just simpler. IL is a heckofalot easier to parse than C# source code. And it is language agnostic.