public void CompilationChain_UsingRebinding_Directive() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); session.Execute(@"using System.Diagnostics;"); session.Execute(@" #r """ + typeof(Process).Assembly.Location + @""" Process.GetCurrentProcess()"); }
public void CompilationChain_UsingRebinding_AddReference() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); session.Execute(@"using System.Diagnostics;"); session.AddReference(typeof(Process).Assembly); session.Execute(@"Process.GetCurrentProcess()"); }
public void CompilationChain_GlobalImportsRebinding() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); session.ImportNamespace("System.Diagnostics"); ScriptingTestHelpers.AssertCompilationError(session, @" Process.GetCurrentProcess()", // (2,1): error CS0103: The name 'Process' does not exist in the current context Diagnostic(ErrorCode.ERR_NameNotInContext, "Process").WithArguments("Process")); session.Execute(@" #r """ + typeof(Process).Assembly.Location + @""""); session.Execute(@" Process.GetCurrentProcess()"); }
public void CompilationChain_SubmissionSlots() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); session.Execute("using System;"); session.Execute("using static System.Environment;"); session.Execute("int x; x = 1;"); session.Execute("using static System.Math;"); session.Execute("int foo(int a) { return a + 1; } "); #if false Assert.True(session.executionState.submissions.Length >= 2, "Expected two submissions"); session.executionState.submissions.Aggregate(0, (i, sub) => { Assert.Equal(i < 2, sub != null); return i + 1; }); #endif object result; // TODO (tomat): Version is a type and property, but we are not looking for a type, so can we disambiguate? ScriptingTestHelpers.AssertCompilationError(session, "Version", // (1,1): error CS0229: Ambiguity between 'System.Version' and 'System.Environment.Version' Diagnostic(ErrorCode.ERR_AmbigMember, "Version").WithArguments("System.Version", "System.Environment.Version") ); result = session.Execute("new System.Collections.Generic.List<Version>()"); Assert.True(result is List<Version>, "Expected List<Version>"); result = session.Execute("Environment.Version"); Assert.Equal(Environment.Version, result); result = session.Execute("foo(x)"); Assert.Equal(2, result); result = session.Execute("Sin(0)"); Assert.Equal(0.0, result); }
public void HostObjectBinding_DuplicateReferences() { var engine = new CSharpScriptEngine(); var c = new C(); var session = engine.CreateSession(c); session.AddReference(typeof(C).Assembly); session.AddReference(typeof(C).Assembly); // includes mscorlib Assert.Equal(3, session.References.Length); int result = session.Execute<int>("x"); Assert.Equal(1, result); int result2 = session.Execute<int>(@" #r """ + typeof(C).Assembly.Location + @""" #r """ + typeof(C).Assembly.Location + @""" x "); Assert.Equal(1, result); }
public void ReferenceToInvalidType() { var badTypeBytes = TestResources.MetadataTests.Invalid.ClassLayout; var badTypeRef = MetadataReference.CreateFromImage(badTypeBytes.AsImmutableOrNull()); // TODO: enable this with our AssemblyLoader: ResolveEventHandler handler = (_, args) => { if (args.Name.StartsWith("b,", StringComparison.Ordinal)) { return Assembly.Load(badTypeBytes); } return null; }; AppDomain.CurrentDomain.AssemblyResolve += handler; try { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); session.AddReference(badTypeRef); // we shouldn't throw while compiling: var submission = session.CompileSubmission<object>("new S1()"); // we should throw while executing: Assert.Throws<TypeLoadException>(() => submission.Execute()); } finally { AppDomain.CurrentDomain.AssemblyResolve -= handler; } }
public void AssemblyResolution() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); var instance = session.Execute("var x = new { a = 3 }; x"); var type = session.Execute("System.Type.GetType(x.GetType().AssemblyQualifiedName, true)"); Assert.Equal(instance.GetType(), type); }
public void MissingDependency() { var engine = new CSharpScriptEngine(); ScriptingTestHelpers.AssertCompilationError(engine, @" #r ""WindowsBase"" #r ""PresentationCore"" #r ""PresentationFramework"" using System.Windows; System.Collections.IEnumerable w = new Window(); ", // (7,36): error CS0012: The type 'System.ComponentModel.ISupportInitialize' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. // System.Collections.IEnumerable w = new Window(); Diagnostic(ErrorCode.ERR_NoTypeDef, "new Window()").WithArguments("System.ComponentModel.ISupportInitialize", "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"), // (7,36): error CS0012: The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. // System.Collections.IEnumerable w = new Window(); Diagnostic(ErrorCode.ERR_NoTypeDef, "new Window()").WithArguments("System.Windows.Markup.IQueryAmbient", "System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"), // (7,36): error CS0266: Cannot implicitly convert type 'System.Windows.Window' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?) // System.Collections.IEnumerable w = new Window(); Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "new Window()").WithArguments("System.Windows.Window", "System.Collections.IEnumerable")); }
public void References2() { var engine = new CSharpScriptEngine(); engine.SetReferenceSearchPaths(RuntimeEnvironment.GetRuntimeDirectory()); engine.AddReference("System.Core"); engine.AddReference("System.dll"); engine.AddReference(typeof(System.Data.DataSet).Assembly); var session = engine.CreateSession(); var process = (Process)session.Execute(@" #r """ + typeof(System.Xml.Serialization.IXmlSerializable).Assembly.Location + @""" new System.Data.DataSet(); System.Linq.Expressions.Expression.Constant(123); System.Diagnostics.Process.GetCurrentProcess() "); Assert.NotNull(process); }
public void References1() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); session.AddReference(typeof(Process).Assembly.FullName); session.AddReference(typeof(System.Linq.Expressions.Expression).Assembly); var process = (Process)session.Execute(@" #r """ + typeof(System.Data.DataSet).Assembly.Location + @""" #r ""System"" #r """ + typeof(System.Xml.Serialization.IXmlSerializable).Assembly.Location + @""" new System.Data.DataSet(); System.Linq.Expressions.Expression.Constant(123); System.Diagnostics.Process.GetCurrentProcess() "); Assert.NotNull(process); session.AddReference(typeof(System.Xml.XmlDocument).Assembly); var xmlDoc = (System.Xml.XmlDocument)session.Execute(@" new System.Xml.XmlDocument() "); Assert.NotNull(xmlDoc); session.AddReference("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); var color = session.Execute(@" System.Drawing.Color.Coral "); Assert.NotNull(color); session.AddReference(typeof(System.Windows.Forms.Form).Assembly.Location); var form = (System.Windows.Forms.Form)session.Execute(@" new System.Windows.Forms.Form(); "); Assert.NotNull(form); }
public void SearchPaths_BaseDirectory() { var engine = new CSharpScriptEngine(new MetadataReferenceProvider(new Dictionary<string, PortableExecutableReference> { { @"C:\dir\x.dll", (PortableExecutableReference)SystemCoreRef } })); engine.MetadataReferenceResolver = new VirtualizedFileReferenceResolver( existingFullPaths: new[] { @"C:\dir\x.dll" }, baseDirectory: @"C:\foo\bar" ); var session = engine.CreateSession(); var source = @" #r ""x.dll"" using System.Linq; var x = from a in new[] { 1,2,3 } select a + 1; "; var submission = session.CompileSubmission<object>(source, @"C:\dir\a.csx", isInteractive: false); submission.Execute(); }
public void SearchPaths_RemoveDefault() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); // remove default paths: session.SetReferenceSearchPaths(); ScriptingTestHelpers.AssertCompilationError(session, @" #r ""System.Data.dll"" new System.Data.DataSet() ", // (2,1): error CS0006: Metadata file 'System.Data.dll' could not be found // #r "System.Data.dll" Diagnostic(ErrorCode.ERR_NoMetadataFile, @"#r ""System.Data.dll""").WithArguments("System.Data.dll"), // (3,12): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?) // new System.Data.DataSet() Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS, "Data").WithArguments("Data", "System") ); }
public void SearchPaths1() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); session.SetReferenceSearchPaths(RuntimeEnvironment.GetRuntimeDirectory()); object result = session.Execute(@" #r ""System.Data.dll"" #r ""System"" #r """ + typeof(System.Xml.Serialization.IXmlSerializable).GetTypeInfo().Assembly.Location + @""" new System.Data.DataSet() "); Assert.True(result is System.Data.DataSet, "Expected DataSet"); }