public static void test3() { string code1 = @"public void Write(string message, int count) { Console.WriteLine(message); for(int i = 0; i < count; i++) { var xxx = i * 2 * 5 + 100; var str = $""{message} => {xxx}""; Console.WriteLine(str); } }"; string code2 = @"public double Math(double a, double b) { var c = a * b; var str = $""{a} * {b} => {c}""; Console.WriteLine(str); return c; }"; string code = $"{code1} {code2}"; string nameSpace = "Apprentice"; string className = "Writer"; var trans = new Transpiler(); var body = trans.WrapInClass(nameSpace, className, code); // Console.WriteLine($"BODY: {body}"); var compile = trans.Compile(body); Assembly assembly; if (trans.LoadAssembly(compile, out assembly)) { string target = $"{nameSpace}.{className}"; Type type = assembly.GetType(target); var obj = Activator.CreateInstance(type); type.InvokeMember("Write", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { "Hello: World", 3 }); var result = type.InvokeMember("Math", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { 17.0, 3.0 }); var str = $" result => {result}"; Console.WriteLine(str); } }
public static void test2() { var root = new FoComponent("Component"); IFoProperty[] props = { new FoProperty <double>("width", 10), new FoProperty <double>("height", .33), new FoProperty <double>("depth", 22), new FoProperty <double>("area", () => { var width = new FoReference("width"); var height = new FoReference("height"); var depth = new FoReference("depth"); var area = width.GetValue <double>(root) * height.GetValue <double>(root); var volume = area * depth.GetValue <double>(root); return(area); }), new FoProperty <double>("volume", () => { var area = new FoReference("area"); var depth = new FoReference("depth"); var volume = area.GetValue <double>(root) * depth.GetValue <double>(root); return(volume); }) }; root.Properties.AddList(props); var parser = new Parser("depth * (width * height) == volume and width * height == area"); var formula = parser.ReadFormula(); var list = new List <Operator>(); formula.CollectionAll(list); var rawData = list .GroupBy(item => item.Name) .Select(item => item.First()) .Where(item => item is ReferenceOperator) .Select(item => $"{((ReferenceOperator)item).ToReferenceStatement()} ") .ToList <string>(); var data = String.Join("\n", rawData); var json = formula.toJson(); var cSharp = formula.AsCSharp(); var decompile = formula.Decompile(); string code = $"{data}\n\nvar result = {cSharp};\n\n return result;"; // Console.WriteLine($"code: {code}"); string nameSpace = "Apprentice"; string className = "Compute"; string typeName = "bool"; string methodName = "ComputeValue"; string args = "(FoComponent root)"; var trans = new Transpiler(); var body = trans.WrapInClassMethod(nameSpace, className, typeName, methodName, args, code); Console.WriteLine($"==========================="); //Console.WriteLine($"BODY: {body}"); Assembly assembly; var compile = trans.Compile(body); if (trans.LoadAssembly(compile, out assembly)) { string target = $"{nameSpace}.{className}"; Type type = assembly.GetType(target); var obj = Activator.CreateInstance(type); var thanks = type.InvokeMember(methodName, BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { root }); Console.WriteLine($"It Works if the answer is true"); Console.WriteLine($"{methodName} => {thanks}"); var volume = root.Reference <FoProperty <double> >("volume").Value; Console.WriteLine($"Volume => {volume}"); var area = root.Reference <FoProperty <double> >("area").Value; Console.WriteLine($"Area => {area}"); } Console.WriteLine($".........................."); }