Modify and run an existing .NET assembly without saving to disk

Is there a way to change the methods in an existing .NET assembly and execute the assembly without saving it to disk. I would like the functionality to be similar to System.Reflection.Emit.MethodRental, but the namespace Reflection.Emitonly deals with a dynamically created assembly. There are many approaches that include creating a new executable file. or modifying an existing one, but I would like to avoid doing any of these things.

I could use the CLR profiler JR compilers to overwrite the method bodies, but would prefer an approach that could be done in C #. The use case is part of the profiler, which displays program values ​​when the function enters / exits.

+3
source share
2 answers

If you have modified binary content, you can use Assembly.Load(byte[]). However, you cannot modify the loaded assembly — you will have to process the changes separately.

You may also want to run it separately AppDomainif you do it multiple times (if you do it only once, you probably don't need to).

+2
source

Take a look at the “expression trees” that come with .net 3, they also allow you 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 look at Mono.Cecil - this library allows you to change code at runtime.

0

Source: https://habr.com/ru/post/1784492/


All Articles