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


C# Contracts.ScriptPackSession类代码示例

本文整理汇总了C#中ScriptCs.Contracts.ScriptPackSession的典型用法代码示例。如果您正苦于以下问题:C# ScriptPackSession类的具体用法?C# ScriptPackSession怎么用?C# ScriptPackSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ScriptPackSession类属于ScriptCs.Contracts命名空间,在下文中一共展示了ScriptPackSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TheConstructor

 public TheConstructor()
 {
     _scriptPackMock = new Mock<IScriptPack>();
     _contextMock = new Mock<IScriptPackContext>();
     _scriptPackMock.Setup(p => p.GetContext()).Returns(_contextMock.Object);
     _scriptPackSession = new ScriptPackSession(new List<IScriptPack>{_scriptPackMock.Object}, new string[0]);
 }
开发者ID:AsCloud,项目名称:scriptcs,代码行数:7,代码来源:ScriptPackSessionTests.cs

示例2: Execute

        public ScriptResult Execute(string code, string[] scriptArgs, AssemblyReferences references, IEnumerable<string> namespaces,
            ScriptPackSession scriptPackSession)
        {
            Guard.AgainstNullArgument("references", references);
            Guard.AgainstNullArgument("scriptPackSession", scriptPackSession);

            references.PathReferences.UnionWith(scriptPackSession.References);

            SessionState<Evaluator> sessionState;
            if (!scriptPackSession.State.ContainsKey(SessionKey))
            {
                Logger.Debug("Creating session");
                var context = new CompilerContext(new CompilerSettings
                {
                    AssemblyReferences = references.PathReferences.ToList()
                }, new ConsoleReportPrinter());

                var evaluator = new Evaluator(context);
                var allNamespaces = namespaces.Union(scriptPackSession.Namespaces).Distinct();

                var host = _scriptHostFactory.CreateScriptHost(new ScriptPackManager(scriptPackSession.Contexts), scriptArgs);
                MonoHost.SetHost((ScriptHost)host);

                evaluator.ReferenceAssembly(typeof(MonoHost).Assembly);
                evaluator.InteractiveBaseClass = typeof(MonoHost);

                sessionState = new SessionState<Evaluator>
                {
                    References = new AssemblyReferences(references.PathReferences, references.Assemblies),
                    Namespaces = new HashSet<string>(),
                    Session = evaluator
                };

                ImportNamespaces(allNamespaces, sessionState);

                scriptPackSession.State[SessionKey] = sessionState;
            }
            else
            {
                Logger.Debug("Reusing existing session");
                sessionState = (SessionState<Evaluator>)scriptPackSession.State[SessionKey];

                var newReferences = sessionState.References == null ? references : references.Except(sessionState.References);
                foreach (var reference in newReferences.PathReferences)
                {
                    Logger.DebugFormat("Adding reference to {0}", reference);
                    sessionState.Session.LoadAssembly(reference);
                }

                sessionState.References = new AssemblyReferences(references.PathReferences, references.Assemblies);

                var newNamespaces = sessionState.Namespaces == null ? namespaces : namespaces.Except(sessionState.Namespaces);
                ImportNamespaces(newNamespaces, sessionState);
            }

            Logger.Debug("Starting execution");
            var result = Execute(code, sessionState.Session);
            Logger.Debug("Finished execution");
            return result;
        }
开发者ID:selony,项目名称:scriptcs,代码行数:60,代码来源:MonoScriptEngine.cs

示例3: ShouldThrowExceptionThrownByScriptWhenErrorOccurs

            public void ShouldThrowExceptionThrownByScriptWhenErrorOccurs(
                [NoAutoProperties] RoslynScriptDebuggerEngine scriptEngine)
            {
                // Arrange
                var lines = new List<string>
                {
                    "using System;",
                    @"throw new InvalidOperationException(""InvalidOperationExceptionMessage."");"
                };

                var code = string.Join(Environment.NewLine, lines);
                var session = new ScriptPackSession(Enumerable.Empty<IScriptPack>());

                // Act
                var exception = Assert.Throws<ScriptExecutionException>(() =>
                    scriptEngine.Execute(
                        code,
                        new string[0],
                        Enumerable.Empty<string>(),
                        Enumerable.Empty<string>(),
                        session));

                // Assert
                exception.Message.ShouldContain("at Submission#0");
                exception.Message.ShouldContain("Exception Message: InvalidOperationExceptionMessage.");
            }
开发者ID:johnduhart,项目名称:scriptcs,代码行数:26,代码来源:RoslynScriptDebuggerEngineTests.cs

示例4: Execute

        public ScriptResult Execute(string code, string[] scriptArgs, AssemblyReferences references, IEnumerable<string> namespaces,
            ScriptPackSession scriptPackSession)
        {
            var debugLine = code.Substring(0, code.IndexOf(".lol\"") + 5);
            code = code.Replace(debugLine, string.Empty).TrimStart();

            var compiler = new LOLCodeCodeProvider();
            var cparam = new CompilerParameters
            {
                GenerateInMemory = true,
                MainClass = "Program",
                OutputAssembly = "lolcode.dll"
            };
            cparam.ReferencedAssemblies.AddRange(references.PathReferences != null ? references.PathReferences.ToArray() : new string[0]);
            var results = compiler.CompileAssemblyFromSource(cparam, code);

            var x = results.CompiledAssembly.GetReferencedAssemblies();

            var startupType = results.CompiledAssembly.GetType("Program", true, true);
            var instance = Activator.CreateInstance(startupType, false);
            var entryPoint = results.CompiledAssembly.EntryPoint;

            var result = entryPoint.Invoke(instance, new object[] { });

            return new ScriptResult(result);
        }
开发者ID:modulexcite,项目名称:ScriptCs.Engine.LolCode,代码行数:26,代码来源:LolCodeEngine.cs

示例5: ShouldReturnOnlyLastValueOfVariablesDeclaredManyTimes

            public void ShouldReturnOnlyLastValueOfVariablesDeclaredManyTimes([NoAutoProperties]RoslynReplEngine engine, ScriptPackSession scriptPackSession)
            {
                var session = new SessionState<Session> { Session = new ScriptEngine().CreateSession() };
                scriptPackSession.State[RoslynScriptEngine.SessionKey] = session;

                engine.Execute("int x = 1;", new string[0], new AssemblyReferences(), Enumerable.Empty<string>(), scriptPackSession);
                engine.Execute("int x = 2;", new string[0], new AssemblyReferences(), Enumerable.Empty<string>(), scriptPackSession);

                engine.GetLocalVariables(scriptPackSession).ShouldEqual(new Collection<string> { "System.Int32 x = 2" });
            }
开发者ID:jrusbatch,项目名称:scriptcs,代码行数:10,代码来源:RoslynReplEngineTests.cs

示例6: ShouldReturnOnlyLastValueOfVariablesDeclaredManyTimes

            public void ShouldReturnOnlyLastValueOfVariablesDeclaredManyTimes([NoAutoProperties]MonoScriptEngine engine, ScriptPackSession scriptPackSession)
            {
                var session = new SessionState<Evaluator> { Session = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter())) };
                scriptPackSession.State[MonoScriptEngine.SessionKey] = session;

                engine.Execute("int x = 1;", new string[0], new AssemblyReferences(), Enumerable.Empty<string>(), scriptPackSession);
                engine.Execute("int x = 2;", new string[0], new AssemblyReferences(), Enumerable.Empty<string>(), scriptPackSession);

                engine.GetLocalVariables(scriptPackSession).ShouldEqual(new Collection<string> { "int x = 2" });
            }
开发者ID:jrusbatch,项目名称:scriptcs,代码行数:10,代码来源:MonoScriptEngineTests.cs

示例7: Execute

        public ScriptResult Execute(string code, string[] scriptArgs, IEnumerable<string> references, IEnumerable<string> namespaces, ScriptPackSession scriptPackSession)
        {
            Guard.AgainstNullArgument("scriptPackSession", scriptPackSession);

            Logger.Debug("Starting to create execution components");
            Logger.Debug("Creating script host");

            var distinctReferences = references.Union(scriptPackSession.References).Distinct().ToList();
            SessionState<Session> sessionState;

            if (!scriptPackSession.State.ContainsKey(SessionKey))
            {
                var host = _scriptHostFactory.CreateScriptHost(new ScriptPackManager(scriptPackSession.Contexts), scriptArgs);
                Logger.Debug("Creating session");
                var session = ScriptEngine.CreateSession(host);

                foreach (var reference in distinctReferences)
                {
                    Logger.DebugFormat("Adding reference to {0}", reference);
                    session.AddReference(reference);
                }

                foreach (var @namespace in namespaces.Union(scriptPackSession.Namespaces).Distinct())
                {
                    if (@namespace.Contains("ScriptCs.ReplCommand.Pack")) continue;
                    Logger.DebugFormat("Importing namespace {0}", @namespace);
                    session.ImportNamespace(@namespace);
                }

                sessionState = new SessionState<Session> { References = distinctReferences, Session = session };
                scriptPackSession.State[SessionKey] = sessionState;
            }
            else
            {
                Logger.Debug("Reusing existing session");
                sessionState = (SessionState<Session>)scriptPackSession.State[SessionKey];

                var newReferences = sessionState.References == null || !sessionState.References.Any() ? distinctReferences : distinctReferences.Except(sessionState.References);
                if (newReferences.Any())
                {
                    foreach (var reference in newReferences)
                    {
                        Logger.DebugFormat("Adding reference to {0}", reference);
                        sessionState.Session.AddReference(reference);
                    }

                    sessionState.References = newReferences;
                }
            }

            Logger.Debug("Starting execution");
            var result = Execute(code, sessionState.Session);
            Logger.Debug("Finished execution");
            return result;
        }
开发者ID:ktroach,项目名称:scriptcs-replcommand-infra,代码行数:55,代码来源:RoslynScriptEngine.cs

示例8: TheInitializeMethod

 public TheInitializeMethod()
 {
     _contextMock1 = new Mock<IScriptPackContext>();
     _contextMock2 = new Mock<IScriptPackContext>();
     _scriptPackMock1 = new Mock<IScriptPack>();
     _scriptPackMock1.Setup(p => p.GetContext()).Returns(_contextMock1.Object);
     _scriptPackMock2 = new Mock<IScriptPack>();
     _scriptPackMock2.Setup(p => p.GetContext()).Returns(_contextMock2.Object);
     _scriptPackSession = new ScriptPackSession(new List<IScriptPack> { _scriptPackMock1.Object, _scriptPackMock2.Object }, new string[0]);
     _scriptPackSession.InitializePacks();
 }
开发者ID:AsCloud,项目名称:scriptcs,代码行数:11,代码来源:ScriptPackSessionTests.cs

示例9: GetLocalVariables

        public static ICollection<string> GetLocalVariables(this IReplEngine replEngine, string sessionKey,
            ScriptPackSession scriptPackSession)
        {
            if (scriptPackSession != null && scriptPackSession.State.ContainsKey(sessionKey))
            {
                var sessionState = (SessionState<ScriptState>)scriptPackSession.State[sessionKey];
                return sessionState.Session.Variables.Select(x => string.Format("{0} {1}", x.Type, x.Name)).ToArray();
            }

            return new string[0];
        }
开发者ID:modulexcite,项目名称:scriptcs-engines,代码行数:11,代码来源:ReplEngineExtensions.cs

示例10: ShouldReturnDeclaredVariables

            public void ShouldReturnDeclaredVariables([NoAutoProperties]CSharpReplEngine engine, ScriptPackSession scriptPackSession)
            {
                var session = new SessionState<ScriptState> { Session = CSharpScript.Run("") };
                scriptPackSession.State[CommonScriptEngine.SessionKey] = session;

                engine.Execute("int x = 1;", new string[0], new AssemblyReferences(), Enumerable.Empty<string>(),
                    scriptPackSession);
                engine.Execute(@"var y = ""www"";", new string[0], new AssemblyReferences(), Enumerable.Empty<string>(),
                scriptPackSession);

                engine.GetLocalVariables(scriptPackSession).ShouldEqual(new Collection<string> { "System.Int32 x", "System.String y" });
            }
开发者ID:scriptcs,项目名称:scriptcs,代码行数:12,代码来源:CSharpReplEngineTests.cs

示例11: ShouldReturn0VariablesAfterReset

            public void ShouldReturn0VariablesAfterReset([NoAutoProperties]RoslynReplEngine engine, ScriptPackSession scriptPackSession)
            {
                var session = new SessionState<Session> { Session = new ScriptEngine().CreateSession() };
                scriptPackSession.State[RoslynScriptEngine.SessionKey] = session;

                engine.Execute("int x = 1;", new string[0], new AssemblyReferences(), Enumerable.Empty<string>(),
                    scriptPackSession);
                engine.Execute(@"var y = ""www"";", new string[0], new AssemblyReferences(), Enumerable.Empty<string>(),
                scriptPackSession);

                scriptPackSession.State[RoslynScriptEngine.SessionKey] = new SessionState<Session> { Session = new ScriptEngine().CreateSession() };

                engine.GetLocalVariables(scriptPackSession).ShouldBeEmpty();
            }
开发者ID:jrusbatch,项目名称:scriptcs,代码行数:14,代码来源:RoslynReplEngineTests.cs

示例12: GetLocalVariables

        public ICollection<string> GetLocalVariables(ScriptPackSession scriptPackSession)
        {
            if (scriptPackSession != null && scriptPackSession.State.ContainsKey(SessionKey))
            {
                var sessionState = (SessionState<Evaluator>)scriptPackSession.State[SessionKey];
                var vars = sessionState.Session.GetVars();
                if (!string.IsNullOrWhiteSpace(vars) && vars.Contains(Environment.NewLine))
                {
                    return vars.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                }
            }

            return new Collection<string>();
        }
开发者ID:JamesLinus,项目名称:scriptcs,代码行数:14,代码来源:MonoScriptEngine.cs

示例13: ShouldReturn0VariablesAfterReset

            public void ShouldReturn0VariablesAfterReset([NoAutoProperties]MonoScriptEngine engine, ScriptPackSession scriptPackSession)
            {
                var session = new SessionState<Evaluator> { Session = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter())) };
                scriptPackSession.State[MonoScriptEngine.SessionKey] = session;

                engine.Execute("int x = 1;", new string[0], new AssemblyReferences(), Enumerable.Empty<string>(),
                    scriptPackSession);
                engine.Execute(@"var y = ""www"";", new string[0], new AssemblyReferences(), Enumerable.Empty<string>(),
    scriptPackSession);

                scriptPackSession.State[MonoScriptEngine.SessionKey] = new SessionState<Evaluator> { Session = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter())) };

                engine.GetLocalVariables(scriptPackSession).ShouldBeEmpty();
            }
开发者ID:jrusbatch,项目名称:scriptcs,代码行数:14,代码来源:MonoScriptEngineTests.cs

示例14: ShouldSetIsCompleteSubmissionToFalseIfCodeIsMissingCurlyBracket

            public void ShouldSetIsCompleteSubmissionToFalseIfCodeIsMissingCurlyBracket(
                [NoAutoProperties] CSharpReplEngine engine, ScriptPackSession scriptPackSession)
            {
                // Arrange
                const string Code = "class test {";

                var session = new SessionState<ScriptState> { Session = CSharpScript.Run("") };
                scriptPackSession.State[CommonScriptEngine.SessionKey] = session;
                var refs = new AssemblyReferences(new[] { "System" });

                // Act
                var result = engine.Execute(
                    Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);

                // Assert
                result.IsCompleteSubmission.ShouldBeFalse();
            }
开发者ID:scriptcs,项目名称:scriptcs,代码行数:17,代码来源:CSharpReplEngineTests.cs

示例15: ShouldSetIsCompleteSubmissionToFalseIfCodeIsMissingParenthesis

            public void ShouldSetIsCompleteSubmissionToFalseIfCodeIsMissingParenthesis(
                [NoAutoProperties] RoslynReplEngine engine,
                ScriptPackSession scriptPackSession)
            {
                // Arrange
                const string Code = "System.Diagnostics.Debug.WriteLine(\"a\"";

                var session = new SessionState<Session> { Session = new ScriptEngine().CreateSession() };
                scriptPackSession.State[RoslynScriptEngine.SessionKey] = session;
                var refs = new AssemblyReferences(new[] { "System" });

                // Act
                var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty<string>(), scriptPackSession);

                // Assert
                result.IsCompleteSubmission.ShouldBeFalse();
            }
开发者ID:AsCloud,项目名称:scriptcs,代码行数:17,代码来源:RoslynReplEngineTests.cs


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