How can I programmatically (using reflection?) Change the body of a method and save my changes back to disk

I can get the method body quite easily using reflection

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

How can I programmatically change the body of a method and save my changes back to disk?

+3
source share
3 answers

AFAIK, you cannot.

With reflection, you changed the object in memory that was created from a binary file loaded and optimized by the CLR at runtime.

EDIT

This question contains additional information about this.

Modify existing .NET collections

+3
source

Friend is a good lib for this: https://bitbucket.org/0xd4d/dnlib

, Mono.Cecil.

+2

you cannot do this without third-party libraries. take a look at: http://www.mono-project.com/Cecil

+1
source

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


All Articles