Mono.Cecil 可以修改 AppDomain 中已加载的代码吗?
我想在运行时向某个类添加一些行为。我知道如何在运行时使用 Reflection.Emit 进行子类化,但这还不够。根据某些外部配置,我需要在类型 T 的方法中注入操作码,以便从它继承的所有类都会自动获得此行为。 (我无法使用.NET Profiling API)。
Mono.Cecil 可以完成这样的事情吗?
如果无法修改已加载程序集上的代码,那么如果我可以在加载程序集之前进行修改,然后将修改后的程序集加载到内存中,那很好,但我不知道如何控制程序集加载。
I want to add some behavior to a certain class at runtime. I know how to subclass at runtime using Reflection.Emit but that's not enough. Depending on some external configuration I need to inject opcodes in a method on a type T so all classes that inherit from it automatically gain this behavior. (I can't use the .NET Profiling API).
Can something like this be done with Mono.Cecil?
If it isn't possible to modify code on a loaded assembly, it is fine If I can make the modifications before the assembly is loaded and then load the modified assembly in memory, but I don't know how I can control assembly loading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以,Cecil 无法修改已加载的程序集。您必须在实际加载组件之前对其进行检测。
您对如何解析程序集没有太多控制权。如果您将程序集隐藏在您的私人文件夹中,则可以挂钩 AppDomain.AssemblyResolve,然后在加载它们之前进行检测。
Nope, Cecil can not modify a loaded assembly. You have to instrument assemblies before they are actually loaded.
You don't have much control over how assemblies are resolved. You can hook into AppDomain.AssemblyResolve if you hide the assemblies in a private folder of yours, and instrument then before loading them.
正如 JB 上面所说-
您可以创建一个 Resolve 事件处理程序 - 类似于 PSeudoHooking。
在加载程序集之前,您可以进行更改,然后一旦更改完成,解析程序集就会继续加载更改后的程序集。
我使用此方法从内存流解析嵌入式 Dll。
As JB Says above-
You can create a Resolve Event handler - which would be like PSeudoHooking.
And before the assembly is loaded, you make your changes, and then once the changes are done, the Resolve Assembly then continues on to load the changed assembly.
I use this method for resolving Embedded Dll's from Memory Streams.