可以Mono.Cecil能修改在AppDomain中已经装入code?Cecil、Mono、code、AppDomain

2023-09-03 04:56:53 作者:Fetter(自作多情)

我想添加一些行为,以某一类在运行时。我知道如何在继承使用Reflection.Emit的运行但那还不够,根据一些外部的配置,我需要一个类型T,以便继承它会自动获得这种行为的所有类注入运codeS中的方法。(我不能使用.NET分析API)

I want to add some behavior to a certain class at runtime. I know how to subclass at runtime using Reflection.Emit but thats 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 cant use the .NET Profiling API)

可以像这样用Mono.Cecil能做到呢?

Can something like this be done with Mono.Cecil?

如果它不是可以修改code上加载的程序集,这是好的,如果该程序集加载之前,我可以做的修改,然后加载在内存中修改组件,但我不知道我可以控制组件负载。

If it isnt 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 dont know how I can control assembly loading.

推荐答案

不,塞西尔不能修改加载的程序集。你必须仪器组件其实都是装了。

Nope, Cecil can not modify a loaded assembly. You have to instrument assemblies before they are actually loaded.

您没有太多的控制权如何组件解决。您可以挂接到AppDomain.AssemblyResolve如果隐藏在你的私人文件夹中的程序集和仪器然后加载它们。

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.