是否可以编写一个动态生成新类并用新类修补自身的程序集?

发布于 2024-07-21 06:04:24 字数 53 浏览 3 评论 0原文

是否可以编写一个动态生成/发出新类并修补自身以包含新类的程序集?

如何?

Is it possible to write an assembly which dynamically generates/emits a new class and patches itself to include the new class?

How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

万劫不复 2024-07-28 06:04:24

我在这里以另一种方式问这个问题: 使用AssemblyBuilder,如何使所有或任何引用的程序集嵌入而不是链接到保存的程序集中?

使用动态生成的代码修补现有的 dll 会产生与嵌入原始代码相同的结果动态生成的代码中包含 .dll - 包含两者内容的单个程序集。

无论如何,为了消除依赖性并将多个程序集的内容打包到一个程序集中,ILMerge 实用程序似乎是最优雅的解决方案。

唯一的问题是合并的 dll 中生成的类型与两个原始 dll 中的相同类型不兼容。 例如,如果原始 DLL 发出一个新程序集,将其与自身合并,然后加载新程序集……它不能使用自己的类型来引用新程序集中与任一程序集中相同类型的内容。原始组件。

换句话说:
[dll_generator] 中的类 A 引用 [dll_1]。 A类生成[dll_2],它基于并且当然也引用了[dll_1]。 A 类调用 ILMerge 将 [dll_2] 与其依赖项 [dll_1] 组合起来,生成 [dll_merged]。 [dll_merged] 中的任何类型都与 [dll_1] 和 [dll_2] 中的任何原始类型兼容,因此,如果类 A 加载 [dll_merged] 并尝试对其执行任何涉及从其原始引用到 [ 的文字类型名称的操作, dll_1],它失败,因为类型不兼容。 类 A 可以使用 [dll_merged] 中的类型的唯一方法是按名称加载它们并完全使用“类型”对象和反射,或者针对新的 [dll_merged] 动态编译源代码。

I asked this question another way here: Using AssemblyBuilder, how can I make all or any of the referenced assemblies embedded instead of linked in the saved assembly?

Patching the existing dll with the dynamically generated code would result in the same thing as embedding the original dll in the dynamically generated code -- a single assembly with the contents of both.

It seems that one way or another, to eliminate dependencies and pack the contents of multiple assemblies into one, the ILMerge utility is the most elegant solution.

The only problem with it is that the types generated in the merged dll are incompatible with the same types in both of the original dlls. If the original DLL, for example, emits a new assembly, merges it with itself, and loads the new assembly... it cannot use its own types to refer to things in the new assembly which correspond to the same type in either of the original assemblies.

In other words:
Class A in [dll_generator] references [dll_1]. Class A generates [dll_2], which is based on and of course also references [dll_1]. Class A calls ILMerge to combine [dll_2] with its dependency [dll_1] to produce [dll_merged]. None of the types in [dll_merged] are compatible with any of their original types in [dll_1] and [dll_2], so if class A loads [dll_merged] and tries to do anything with it involving literal type names from its original reference to [dll_1], it fails, because the types are incompatible. The only way class A can work with types in [dll_merged] is to load them by name and work entirely with 'Type' objects and reflection -- or dynamically compile source code against the new [dll_merged].

温柔女人霸气范 2024-07-28 06:04:24

做到这一点的最佳方法是使用依赖注入/控制反转,甚至是简单的服务定位器。

您的新程序集将创建一个新的具体实现并注册它,而不是旧的实现。

我确信任何更奇特的东西确实是一个可怕的黑客。

The best way to do this is to use dependency injection / inversion of control or even a simple service locater.

Your new assembly would create a new concrete implementation and register that instead of the old implementation.

I'm certain that anything more exotic would be a terrible hack indeed.

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