當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。