Finding a way to control .Net CIL at run time

In Java, we used the javaagent argument and the ASM utilities (http://asm.ow2.org/) to change the byte code when starting / loading in memory by the class loader. (otherwise add a method call to the method in the class dynamically).

As an example, you can remove all Log4j calls to speed up the application (http://surguy.net/articles/removing-log-messages.xml).

I am trying to figure out how to do the same process at runtime with C # /. Net. I saw that you can manipulate CIL for .Net, but I have not found an example of this at runtime.

System.Reflection.Emit seems to be the closest .Net equivalent to where you can dynamically create classes, but is there a way to add or override existing classes using this?

+6
source share
3 answers

I have never used Mono.Cecil to generate dynamic code (this makes your life very easy if you want to build tools).

In .Net, if you want to generate code, you can use System.CodeDom and System.Reflection.Emit . One useful class that allows you to dynamically introduce methods is DynamicMethod .

+2
source

Check out the newer features in .net 4, I think most of your search is in the System.Dynamic namespace.

Send a message to DuckTyping

+1
source

It has been a while since I looked at it (I basically look like a Java rabbit), but I think that there was something called Cecil in the Mono project, which at least is some of this.

0
source

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


All Articles