0
try
{
    Type type = typeof(Functions);

    MethodInfo method = type.GetMethod(msg);
    Functions c = new Functions();

    ParameterInfo[] pars = method.GetParameters();
    foreach (ParameterInfo p in pars)
    {
        send(p.ParameterType);
    }
    string result = (string)method.Invoke(c, null);

    send(result);
}
catch (Exception ex)
{
    send(ex.Message);
}

So msg is string, I want to call function with parameters, for example like this:

string msg = "print(\"yes\")";

public class Functions
{
    public string print(string text)
    {
        send(text);
    }
}

Currently if msg is equal to "print" it can execute simple print() function in Functions class:

public class Functions
{
    public string print()
    {
        return "print() called";
    }
}

code

But I don't know how to make it execute func with parameters, also get parameters from msg.

  • You can either parse the string yourself or use https://stackoverflow.com/questions/825107/how-to-convert-string-to-code-in-c-sharp – Yuriy Faktorovich Jan 03 '20 at 21:41
  • What about [Scripting API Samples](https://github.com/dotnet/roslyn/wiki/Scripting-API-Samples) – Jeroen van Langen Jan 03 '20 at 22:24
  • "get parameters from msg" is way too broad - so you have to figure out exactly what your requirements are and probably ask separate question on it. "execute func with parameters" is on other hand is properly scoped for SO and already answered in the linked duplicate. – Alexei Levenkov Jan 03 '20 at 23:24

0 Answers0