Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compile and run C# code string sent from QM to the LA HTTP server
#27
How to modify the GetMethod function to automatically obtain the type and quantity of parameters and return values?
I have searched for a lot of information and still haven't found a solution. It is difficult to understand the usage of delegates here. Huh

_____________________________________________________________________________________________
string code = """
{}public class Class1
{
    public static int Add(int a, int b)
    { return a + b; }
}
""";
var c = CsScript.Compile(code);

var f = c.GetMethod<Func<int, int, int>>("Class1", "Add");
print.it(f(2, 5));
_____________________________________________________________________________________________
string code = """
{}public class Class2
{
    public static string Join(string a, string b, int c)
    { return a + b + c; }
}
""";
var c = CsScript.Compile(code);

var f = c.GetMethod<Func<string, string, int, string>>("Class2", "Join");
print.it(f("hello ", "world ", 123));
_____________________________________________________________________________________________
string code = """
{}public class Class3 {
    public static string Hi(string name) {
        string r = $"hello {name} ";
        return r;
    }
}
""";
var c = CsScript.Compile(code);

var f = c.GetMethod<Func<string, string>>("Class3", "Hi");
print.it(f("Lisa"));

Http server Functions Corresponding modifications are also required

static partial class Functions {
    public static int Temp(string code, string cla, string fun, xxx) {
        
        var c = CsScript.Compile(code);
        var f = c.GetMethod<Func<xxx>>(cla, fun);
        return f(xxx);
        
    }
}


Messages In This Thread
RE: Compile and execute C# code from string at run time - by Davider - 06-22-2023, 10:40 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)