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
#26
Thank you very much for your sharing. It's awesome that we don't need to download the compiler through nuget anymore!

In order to speed up the execution, I converted the CsScript class to a library and generated a dll.

Then I created a function named "Temp" and executed the sample code below. The speed is very fast,
however, I don't know how to make it support various types of parameters and return values. The following is the operation demonstration.

 If it could be simplified to the following way of calling, it would be very convenient.
Code:
Copy      Help
out LaHttpCall(F"code={_s}" "fun=Add" "a=6" "b=7")
out LaHttpCall(F"code={_s}" "fun=Hi" "name=Lisa")

[Image: a.gif]


Macro Macro7
 
Code:
Copy      Help
_s=
;{}public class Class1
;{
;,public static int Add(int a, int b)
;,{ return a + b; }
;}

out LaHttpCall("Temp" F"code={_s}" "cla=Class1" "fun=Add" "a=6" "b=7")

;__________________________________________________________________Todo: make HSFun_Temp.cs support various types of parameters and return values
_s=
;{}public class Class8 {     
;,public static string Hi(string name) {
;,,string r = $"hello {name} ";
;,,Console.WriteLine(r);
;,,return r;
;,}     
;}

;out LaHttpCall("Temp" F"code={_s}" "cla=Class8" "fun=Hi" "name=Lisa") ;;Todo:
 
Code:
Copy      Help
// class "HSFun_Temp.cs"
/*/ r CsScript.dll; /*/

//Todo: make Temp support various types of parameters and return values


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


Messages In This Thread
RE: Compile and execute C# code from string at run time - by Davider - 06-21-2023, 11:03 PM

Forum Jump:


Users browsing this thread: 8 Guest(s)