开发者

Modify and run existing .NET assembly without saving to disk

Is there any way to modify methods in an existing .NET assembly, and execute the assembly without saving it to disk. I would like functionality similar to System.Reflection.Emit.MethodRental but the Reflection.Emit namespace only deals with dynamically created assemblie开发者_StackOverflows. There are many approaches that involve creating a new executable. or modifying an existing one, but I would like to avoid doing either of these.

I could use the CLR profiler's JIT compilation hooks to rewrite the method bodies as well, but would prefer an approach that could be done in C#. The use case is as part of a profiler that outputs program values at function entrance/exit.


If you have the modified binary contents you can use Assembly.Load(byte[]). However, you can't modify a loaded assembly - you'd have to handle the modifications separately.

You might also want to look at running this in a separate AppDomain if you are doing it repeatedly (if doing it just once, you probably don't need to).


Take a look at "Expressions trees" that comes with .net 3, they are also allows to create dynamic methods http://blogs.msdn.com/b/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx

Also take a look at Mono.Cecil - this library allows to change code at runtime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜