当前位置: 首页>>代码示例>>C#>>正文


C# Session.CompileSubmission方法代码示例

本文整理汇总了C#中Session.CompileSubmission方法的典型用法代码示例。如果您正苦于以下问题:C# Session.CompileSubmission方法的具体用法?C# Session.CompileSubmission怎么用?C# Session.CompileSubmission使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Session的用法示例。


在下文中一共展示了Session.CompileSubmission方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CompileAndExecute

        private ScriptResult CompileAndExecute(string code, Session session)
        {
            Logger.Debug("Compiling submission");
            try
            {
                var submission = session.CompileSubmission<object>(code);

                using (var exeStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    var result = submission.Compilation.Emit(exeStream, pdbStream: pdbStream);

                    if (result.Success)
                    {
                        Logger.Debug("Compilation was successful.");

                        var assembly = LoadAssembly(exeStream.ToArray(), pdbStream.ToArray());

                        return InvokeEntryPointMethod(session, assembly);
                    }

                    var errors = string.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString()));
                    
                    Logger.ErrorFormat("Error occurred when compiling: {0})", errors);

                    return new ScriptResult(compilationException: new ScriptCompilationException(errors));
                }
            }
            catch (Exception compileException)
            {
                //we catch Exception rather than CompilationErrorException because there might be issues with assembly loading too
                return new ScriptResult(compilationException: new ScriptCompilationException(compileException.Message, compileException));
            }
        }
开发者ID:selony,项目名称:scriptcs,代码行数:34,代码来源:RoslynScriptCompilerEngine.cs

示例2: Execute

        protected override object Execute(string code, Session session)
        {
            _logger.Debug("Compiling submission");
            var submission = session.CompileSubmission<object>(code);
            var exeBytes = new byte[0];
            var pdbBytes = new byte[0];
            var compileSuccess = false;

            using (var exeStream = new MemoryStream())
            using (var pdbStream = new MemoryStream())
            {
                var result = submission.Compilation.Emit(exeStream, pdbStream: pdbStream);
                compileSuccess = result.Success;

                if (result.Success)
                {
                    _logger.Debug("Compilation was successful.");
                    exeBytes = exeStream.ToArray();
                    pdbBytes = pdbStream.ToArray();
                }
                else
                {
                    var errors = String.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString()));
                    _logger.ErrorFormat("Error occurred when compiling: {0})", errors);
                }
            }

            if (compileSuccess)
            {
                _logger.Debug("Loading assembly into appdomain.");
                var assembly = AppDomain.CurrentDomain.Load(exeBytes, pdbBytes);
                _logger.Debug("Retrieving compiled script class (reflection).");
                var type = assembly.GetType(CompiledScriptClass);
                _logger.Debug("Retrieving compiled script method (reflection).");
                var method = type.GetMethod(CompiledScriptMethod, BindingFlags.Static | BindingFlags.Public);

                try
                {
                    _logger.Debug("Invoking method.");
                    return method.Invoke(null, new[] { session });
                }
                catch (Exception e)
                {
                    _logger.Error("An error occurred when executing the scripts.");
                    var message =
                        string.Format(
                        "Exception Message: {0} {1}Stack Trace:{2}",
                        e.InnerException.Message,
                        Environment.NewLine,
                        e.InnerException.StackTrace);
                    throw new ScriptExecutionException(message);
                }
            }
            return null;
        }
开发者ID:joeriks,项目名称:scriptcs,代码行数:55,代码来源:RoslynScriptDebuggerEngine.cs

示例3: Execute

        protected override void Execute(string code, Session session)
        {
            var submission = session.CompileSubmission<object>(code);
            var exeBytes = new byte[0];
            var pdbBytes = new byte[0];
            var compileSuccess = false;

            using (var exeStream = new MemoryStream())
            using (var pdbStream = new MemoryStream())
            {
                var result = submission.Compilation.Emit(exeStream, pdbStream: pdbStream);
                compileSuccess = result.Success;

                if (result.Success)
                {
                    exeBytes = exeStream.ToArray();
                    pdbBytes = pdbStream.ToArray();
                }
                else
                {
                    var errors = String.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString()));
                    Console.WriteLine(errors);
                }
            }

            if (compileSuccess)
            {
                var assembly = AppDomain.CurrentDomain.Load(exeBytes, pdbBytes);
                var type = assembly.GetType(CompiledScriptClass);
                var method = type.GetMethod(CompiledScriptMethod, BindingFlags.Static | BindingFlags.Public);

                try
                {
                    method.Invoke(null, new[] { session });
                }
                catch (Exception e)
                {
                    var message =
                        string.Format(
                        "Exception Message: {0} {1}Stack Trace:{2}",
                        e.InnerException.Message,
                        Environment.NewLine,
                        e.InnerException.StackTrace);
                    throw new ScriptExecutionException(message);
                }
            }
        }
开发者ID:bangoker,项目名称:scriptcs,代码行数:47,代码来源:RoslynScriptDebuggerEngine.cs

示例4: CompileAndExecute

        private void CompileAndExecute(string code, Session session, ScriptResult scriptResult)
        {
            Submission<object> submission = null;

            Logger.Debug("Compiling submission");
            try
            {
                submission = session.CompileSubmission<object>(code);

                var exeBytes = new byte[0];
                var pdbBytes = new byte[0];
                var compileSuccess = false;

                using (var exeStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    var result = submission.Compilation.Emit(exeStream, pdbStream: pdbStream);
                    compileSuccess = result.Success;

                    if (result.Success)
                    {
                        Logger.Debug("Compilation was successful.");
                        exeBytes = exeStream.ToArray();
                        pdbBytes = pdbStream.ToArray();
                    }
                    else
                    {
                        var errors = string.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString()));
                        Logger.ErrorFormat("Error occurred when compiling: {0})", errors);
                    }
                }

                if (compileSuccess)
                {
                    var assembly = LoadAssembly(exeBytes, pdbBytes);

                    InvokeEntryPointMethod(session, assembly, scriptResult);
                }
            }
            catch (Exception compileException)
            {
                //we catch Exception rather than CompilationErrorException because there might be issues with assembly loading too
                scriptResult.CompileExceptionInfo =
                    ExceptionDispatchInfo.Capture(new ScriptCompilationException(compileException.Message, compileException));
            }
        }
开发者ID:Jaydeep7,项目名称:scriptcs,代码行数:46,代码来源:RoslynScriptCompilerEngine.cs

示例5: Execute

        protected virtual ScriptResult Execute(string code, Session session)
        {
            Guard.AgainstNullArgument("session", session);

            var result = new ScriptResult();
            try
            {
                var submission = session.CompileSubmission<object>(code);
                try
                {
                    result.ReturnValue = submission.Execute();
                }
                catch (Exception ex)
                {
                    result.ExecuteException = ex;
                }
            }
            catch (Exception ex)
            {
                result.CompileException = ex;
            }
            return result;
        }
开发者ID:7sharp9,项目名称:scriptcs,代码行数:23,代码来源:RoslynScriptEngine.cs

示例6: Execute

        protected virtual ScriptResult Execute(string code, Session session)
        {
            Guard.AgainstNullArgument("session", session);

            var result = new ScriptResult();
            try
            {
                var submission = session.CompileSubmission<object>(code);
                try
                {
                    result.ReturnValue = submission.Execute();
                }
                catch (Exception ex)
                {
                    result.ExecuteExceptionInfo = ExceptionDispatchInfo.Capture(ex);
                }
            }
            catch (Exception ex)
            {
                 result.UpdateClosingExpectation(ex);
                if (!result.IsPendingClosingChar)
                {
                    result.CompileExceptionInfo = ExceptionDispatchInfo.Capture(ex);
                }
            }

            return result;
        }
开发者ID:jden,项目名称:scriptcs,代码行数:28,代码来源:RoslynScriptEngine.cs

示例7: Execute

        protected override ScriptResult Execute(string code, Session session)
        {
            Guard.AgainstNullArgument("session", session);

            var scriptResult = new ScriptResult();
            Submission<object> submission = null;

            this.Logger.Debug("Compiling submission");
            try
            {
                submission = session.CompileSubmission<object>(code);

                var exeBytes = new byte[0];
                var pdbBytes = new byte[0];
                var compileSuccess = false;

                using (var exeStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    var result = submission.Compilation.Emit(exeStream, pdbStream: pdbStream);
                    compileSuccess = result.Success;

                    if (result.Success)
                    {
                        this.Logger.Debug("Compilation was successful.");
                        exeBytes = exeStream.ToArray();
                        pdbBytes = pdbStream.ToArray();
                    }
                    else
                    {
                        var errors = String.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString()));
                        this.Logger.ErrorFormat("Error occurred when compiling: {0})", errors);
                    }
                }

                if (compileSuccess)
                {
                    var assembly = this.LoadAssembly(exeBytes, pdbBytes);

                    Logger.Debug("Retrieving compiled script class (reflection).");

                    // the following line can throw NullReferenceException, if that happens it's useful to notify that an error ocurred
                    var type = assembly.GetType(CompiledScriptClass);
                    Logger.Debug("Retrieving compiled script method (reflection).");
                    var method = type.GetMethod(CompiledScriptMethod, BindingFlags.Static | BindingFlags.Public);

                    try
                    {
                        this.Logger.Debug("Invoking method.");
                        scriptResult.ReturnValue = method.Invoke(null, new[] {session});
                    }
                    catch (Exception executeException)
                    {
                        var ex = executeException.InnerException ?? executeException;
                        scriptResult.ExecuteExceptionInfo = ExceptionDispatchInfo.Capture(ex);
                        this.Logger.Error("An error occurred when executing the scripts.");
                    }
                }
            }
            catch (Exception compileException)
            {
                //we catch Exception rather than CompilationErrorException because there might be issues with assembly loading too
                scriptResult.CompileExceptionInfo = ExceptionDispatchInfo.Capture(new ScriptCompilationException(compileException.Message, compileException));
            }

            return scriptResult;
        }
开发者ID:jden,项目名称:scriptcs,代码行数:67,代码来源:RoslynScriptCompilerEngine.cs

示例8: Execute

        protected virtual ScriptResult Execute(string code, Session session)
        {
            Guard.AgainstNullArgument("session", session);

            if (string.IsNullOrWhiteSpace(FileName) && !IsCompleteSubmission(code))
            {
                return ScriptResult.Incomplete;
            }

            try
            {
                var submission = session.CompileSubmission<object>(code);

                try
                {
                    return new ScriptResult(returnValue: submission.Execute());
                }
                catch (Exception ex)
                {
                    return new ScriptResult(executionException: ex);
                }
            }
            catch (Exception ex)
            {
                return new ScriptResult(compilationException: ex);
            }
        }
开发者ID:selony,项目名称:scriptcs,代码行数:27,代码来源:RoslynScriptEngine.cs

示例9: Execute

        protected virtual ScriptResult Execute(string code, Session session)
        {
            Guard.AgainstNullArgument("session", session);

            try
            {
                var submission = session.CompileSubmission<object>(code);

                try
                {
                    return new ScriptResult(submission.Execute());
                }
                catch (AggregateException ex)
                {
                    return new ScriptResult(executionException: ex.InnerException);
                }
                catch (Exception ex)
                {
                    return new ScriptResult(executionException: ex);
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.StartsWith(InvalidNamespaceError))
                {
                    var offendingNamespace = Regex.Match(ex.Message, @"\'([^']*)\'").Groups[1].Value;
                    return new ScriptResult(
                        compilationException: ex, invalidNamespaces: new string[1] { offendingNamespace });
                }

                return new ScriptResult(compilationException: ex);
            }
        }
开发者ID:AsCloud,项目名称:scriptcs,代码行数:33,代码来源:RoslynScriptEngine.cs

示例10: Execute

        public static Object Execute(Session  session, string path)
        {
            Submission<object> submission;
            object retrunValue=null;
            string code=null;
            try
            {
                code = System.IO.File.ReadAllText(path);
                submission = session.CompileSubmission<object>(code,path:path);
                
            }
            catch (Exception compileException)
            {
                throw compileException;
            }

            var exeBytes = new byte[0];
            var pdbBytes = new byte[0];
            var compileSuccess = false;

            using (var exeStream = new MemoryStream())
            using (var pdbStream = new MemoryStream())
            {
                var result = submission.Compilation.Emit(exeStream, pdbStream: pdbStream);
              
                compileSuccess = result.Success;

                //File.WriteAllBytes(@"c:\scripts\dynamic.dll", exeBytes.ToArray());

                if (result.Success)
                {
                    Debugger.Current.OutputDebugInfo("Compilation was successful.");
                    exeBytes = exeStream.ToArray();
                    pdbBytes = pdbStream.ToArray();
                }
                else
                {
                    var errors = String.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString()));
                    Debugger.Current.OutputDebugInfo("Error occurred when compiling: {0})", errors);
                }
            }

            if (compileSuccess)
            {
                Debugger.Current.OutputDebugInfo("Loading assembly into appdomain.");
               // if(debuggerDomain==null)
                 //   debuggerDomain = AppDomain.CreateDomain("debuggerdomain");

                var assembly = AppDomain.CurrentDomain.Load(exeBytes, pdbBytes);
                Debugger.Current.OutputDebugInfo("Retrieving compiled script class (reflection).");
                var type = assembly.GetType(CompiledScriptClass);
                Debugger.Current.OutputDebugInfo("Retrieving compiled script method (reflection).");
                var method = type.GetMethod(CompiledScriptMethod, BindingFlags.Static | BindingFlags.Public);

                try
                {
                    Debugger.Current.OutputDebugInfo("Invoking method.");
                     retrunValue = method.Invoke(null, new[] { session });
                }
                catch (Exception executeException)
                {
                    
                    Debugger.Current.OutputDebugInfo("An error occurred when executing the scripts.");
                    var message =
                        string.Format(
                        "Exception Message: {0} {1}Stack Trace:{2}",
                        executeException.InnerException.Message,
                        Environment.NewLine,
                        executeException.InnerException.StackTrace);
                   // AppDomain.Unload(debuggerDomain);
                    debuggerDomain = null;
                    throw executeException;
                }
            }

            return retrunValue ;

        }
开发者ID:rohithkrajan,项目名称:ExtCS,代码行数:78,代码来源:DebuggerScriptEngineSession.cs


注:本文中的Session.CompileSubmission方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。