Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programatically (using reflection?) change a method body and save my changes back to disk

I can get at the method body easily enough using reflection

Type type = assembly.GetType("Lorem.Ipsum.Dolor.Sit");
MethodInfo methodInfo = type.GetMethod("Amet");
MethodBody methodBody = methodInfo.GetMethodBody();

How can I programatically change the method body and save my changes back to disk?

like image 986
Jeffrey LeCours Avatar asked Mar 09 '26 10:03

Jeffrey LeCours


2 Answers

AFAIK, you can't.

With reflection you modified an in-memory object which was produced from a binary loaded and optimized by the CLR at runtime.

EDIT

This question has some more information on this.

Modifying Existing .NET Assemblies

like image 63
kervin Avatar answered Mar 10 '26 23:03

kervin


One another good lib for this: https://bitbucket.org/0xd4d/dnlib

Handles obfuscated assemblies much better comparing to Mono.Cecil.

like image 28
focus Avatar answered Mar 11 '26 00:03

focus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!