Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compile and execute C# code from string at run time
#2
Examples.
 
Code:
Copy      Help
// script ""
/*/ c CsScript.cs; /*/

string code = """
using System;
using Au;

foreach (var v in args) print.it(v);
"""
;

var c = CsScript.Compile(code);
if (c == null) return;

//print.redirectConsoleOutput = true; //need this if the script contains Console.WriteLine and the caller app isn't console
c.Run("command", "line", "arguments");
 
Code:
Copy      Help
// script ""
/*/ c CsScript.cs; /*/
var c = CsScript.Compile("""class Class1 { public static int Add(int a, int b) { return a + b; } }""", true);

print.it(c.Call("Class1", "Add", 1, 4));

var add = c.GetMethod<Func<int, int, int>>("Class1", "Add");
print.it(add(2, 5));
 
Code:
Copy      Help
// script ""
/*/ c CsScript.cs; /*/

var globals = new[] { """
global using System;
global using Au;
"""

};

string code = """
public class Class1 {
public Class1() { print.it("ctor"); }
public int Method(int k) { print.it("Method", k); return k*2; }
}
"""
;

var c = CsScript.Compile(code, true, globals);
if (c == null) return;

var k = c.CreateInstance("Class1");
print.it(k.Method(5));
 
Code:
Copy      Help
// script ""
/// Compile code with a class and call static methods.

/*/ c CsScript.cs; /*/

string code2 = """
using Au;
public class Class1 {
    public static void Print(string s) { print.it(s); }
    public static int Add(int a, int b) { return a + b; }
    public static void F3(out string s, int i = 0) { s = "OUT " + i; }
}
"""
;

var c2 = CsScript.Compile(code2, library: true);
if (c2 == null) return;

var prt = c2.GetMethod<Action<string>>("Class1", "Print");
prt("TEST");
var add = c2.GetMethod<Func<int, int, int>>("Class1", "Add");
print.it(add(2, 5), add(1000, 1));
var f3 = c2.GetMethod<Delegates.D1>("Class1", "F3");
f3(out var s1); print.it(s1);

/// <summary>
///
Examples of delegates for methods where cannot be used <b>Action</b> or <b>Func</b>, for example with in/ref/out/params/optional parameters.
/// </summary>
class Delegates {
    public delegate void D1(out string s, int i = 0);
}
 
Code:
Copy      Help
// script ""
/// Compile code with a class, create a class instance as dynamic, and call functions.

/*/ c CsScript.cs; /*/

string code3 = """
using Au;
public class Class1 {
    int _value;
    public Class1() { }
    public Class1(int value) { _value = value; }
    public void Print(string s) { print.it(s); }
    public int GetValue() { return _value; }
    public void F3(out string s, int i = 0) { s = "OUT " + i; }
    public int Prop { get; set; }
}
"""
;

var c3 = CsScript.Compile(code3, library: true);
if (c3 == null) return;
//var d = c3.CreateInstance("Class1"); //use constructor with 0 paramaters
var d = c3.CreateInstance("Class1", 3); //use constructor with 1 paramater
d.Print("test");
print.it(d.GetValue());
d.F3(out string s2); print.it(s2);
d.Prop = 4; print.it(d.Prop);


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

Forum Jump:


Users browsing this thread: 2 Guest(s)