C # Reflection, method body change

Is it possible to change the body of a method at runtime?

class Person
{
    public void DoSth()
    { Console.WriteLine("Hello!"); }
}

I wanted to have a simple input field (like a text field) where I can write the source code of the method body at runtime.

A text field may contain data such as:

for (int i = 0; i < 5; i++)
     Console.WriteLine(i);

which should be excluded if

new Person().DoSth()

.

Is (or how is this possible) possible in C # (using Reflection)?
Thank you for your help.

EDIT:
If this is not possible, is it possible to create a new method at run time and call it?

+5
source share

All Articles