0

there is a way to create and invoke a function in run time? for instance, how could I do to invoke this function?

    string function = @"public void Test(int num1, int num2)
        {
            int sum = num1 + num2;
            MessageBox.Show(sum.ToString());
        }";

        System.Reflection.MethodInfo method = null; // some code to create a method info from the string function
        method.Invoke(this, new object[] { 10, 20 });

All I could find were samples of System.Reflection.Emit.DynamicMethod, but none use a function in a string

1 Answers1

0

You can have a look at DynamicMethod class. It is fairly easy to use. Dynamic methods are invoked at fairly the same speed as compiled methods.

Gauravsa
  • 6,330
  • 2
  • 21
  • 30