Exemplo n.º 1
0
 /// <summary>
 /// Produces result with simple string as command with a context
 /// </summary>
 /// <typeparam name="TResult">
 /// The result that will be returned
 /// </typeparam>
 /// <param name="context">
 /// The context in which to execute the command
 /// </param>
 /// <param name="command">
 /// The command as c# script
 /// </param>
 /// <returns>
 /// The <see cref="Task"/>
 /// Will return TResult
 /// </returns>
 /// <exception cref="CSharpScript.Exception">
 /// Will return exception if no code, or compilation exception if
 /// </exception>
 public async Task <TResult> ProduceAsync <TResult>(TContext context, string command)
 {
     CommandValidator.ValidateCommandIsNotNullOrEmpty <TResult, TContext>(command);
     return(await Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.EvaluateAsync <TResult>(
                command,
                Options.ScriptOptions,
                context));
 }
Exemplo n.º 2
0
        /// <summary>
        /// The compile method with a globals type
        /// </summary>
        /// <typeparam name="TResult">
        /// Type of the result
        /// </typeparam>
        /// <typeparam name="TContext">
        /// Context in which the command will be executed
        /// </typeparam>
        /// <param name="command">
        /// The command as c# code
        /// </param>
        /// <returns>
        /// Compiled <see cref="Script"/>.
        /// </returns>
        public Script <TResult> CompileScriptWithContext <TResult, TContext>(string command)
        {
            CommandValidator.ValidateCommandIsNotNullOrEmpty <TResult, TContext>(command);
            var script = Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.Create <TResult>(
                command,
                options: Options.ScriptOptions,
                globalsType: typeof(TContext));

            return(CompileScript(script));
        }
Exemplo n.º 3
0
        /// <summary>
        /// The produce from list async.
        /// </summary>
        /// <typeparam name="TResult">
        /// The result that will be returned
        /// </typeparam>
        /// <param name="context">
        /// The context as TGlobal
        /// </param>
        /// <param name="commands">
        /// Array of C# commands to be executed in order within the context
        /// </param>
        /// <returns>
        /// The <see cref="Task"/> with type of TResult.
        /// </returns>
        /// <exception cref="CSharpScript.Exception">
        /// Will throw compilation exception if the code in the string is not correct
        /// </exception>
        public async Task <TResult> ProduceFromListAsync <TResult>(TContext context, params string[] commands)
        {
            CommandValidator.ValidateCommandIsNotNullOrEmpty <TResult, TContext>(commands);
            var state = await Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.RunAsync(
                commands.First(),
                Options.ScriptOptions,
                context);

            foreach (var command in commands.Skip(1))
            {
                state = await state.ContinueWithAsync(command);
            }

            return((TResult)state.ReturnValue);
        }