为什么编译后代码注入比预编译代码注入更好?

发布于 2024-08-20 14:46:40 字数 381 浏览 3 评论 0原文

所以我们都知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

べ繥欢鉨o。 2024-08-27 14:46:40

5年前设计PostSharp时选择后编译的原因是:

  1. 语言不可知论。
  2. 与高级语言(每隔一年进行一次重大更新)相比,MSIL 具有更稳定的规范。
  3. 大多数时候,MSIL 是处理方面时所需的抽象级别。您不需要知道所有等效的构造(想想 f 'using' 和 'try-finally')。
  4. 在 2008 年之前,没有人成功地生产出像样的 C# 编译器。 Mono所遇到的困难已经足够令人印象深刻了,即使他们现在已经迎头赶上了。
  5. 处理二进制文件似乎比处理源代码快得多。
  6. 处理二进制程序集使得执行它成为可能——正在处理的程序集可以自我转换。在 PostSharp Laos 首次发布之前,这是闻所未闻的。

也就是说,C/C++ 的 AOP 实现确实是一个预编译器 (WeaveC),而 Java 中的实现是一个编译器扩展(因为 Java 编译器有许多 OSS 实现)。

-盖尔

The reasons why I chose post-compilation when designing PostSharp 5 years ago are:

  1. Language agnosticism.
  2. MSIL has stabler specifications compared to high-level languages (which have non-trivial updates every second year).
  3. Most of the time, MSIL is the level of abstraction you need when dealing with aspects. You don't need to know all the equivalent constructs (think f 'using' and 'try-finally').
  4. Before 2008, nobody has succeeded in producing a decent C# compiler. The difficulties met by Mono were impressive enough, even if they have caught up now.
  5. Dealing with binary seemed much faster than dealing with source code.
  6. Dealing with a binary assembly makes it possible to execute it -- the assembly being processed can transforme itself. It was unheard before PostSharp Laos was first released.

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

街角迷惘 2024-08-27 14:46:40

从技术上讲,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.

你的他你的她 2024-08-27 14:46:40

如果您要进行预编译,则必须解释您支持的所有不同语言的源文件,然后在将其传递给编译器之前以该语言生成代码。通过后处理,您可以简单地使用反射来检查程序集,无论原始语言是 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.

淑女气质 2024-08-27 14:46:40

只是更简单而已。 IL 比 C# 源代码更容易解​​析。它与语言无关。

It is just simpler. IL is a heckofalot easier to parse than C# source code. And it is language agnostic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文