예제 #1
0
        public static async Task Exemple_Create_Compile_MultiRun()
        {
            try
            {
                string Input = "int a = 10; " +
                               "int b = 2; " +
                               "int Mul = a * b; " +
                               "return Mul > a;";
                // compilation
                Script script     = CSharpScript.Create(code: Input, options: null, globalsType: null);
                var    resCompile = script.Compile();
                // Affichage des erreurs de compilation
                foreach (var diagnostic in resCompile)
                {
                    Console.WriteLine(diagnostic.ToString());
                }

                // 1 er execution
                ScriptState state = await script.RunAsync(globals : null);

                // Affichage du résultat de retour du script
                if (state.ReturnValue != null)
                {
                    Console.WriteLine(state.ReturnValue.ToString());
                }
                else
                {
                    Console.WriteLine("result is null");
                }
                foreach (var v in state.Variables)
                {
                    Console.WriteLine($"{v.Type} {v.Name} = {v.Value}");
                }

                // 2 eme execution
                state = await script.RunAsync(globals : null);

                ScriptVariable varMul = state.GetVariable("Mul");
                Console.WriteLine($"{varMul.Type} {varMul.Name} = {varMul.Value}");

                // 3 eme Execution avec du code en plus, le context précédant reste valide
                state = await state.ContinueWithAsync("int Add; Add = a + b;");

                ScriptVariable varAdd = state.GetVariable("Add");
                Console.WriteLine($"{varAdd.GetType()} {varAdd.Name} = {varAdd.Value}");
                varMul = state.GetVariable("Mul");
                Console.WriteLine($"{varMul.GetType()} {varMul.Name} = {varMul.Value}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception {ex.Message}");
            }

            Console.WriteLine("appuyez sur une touche");
            Console.ReadKey();
        }