public void AssemblySandboxWin32Call() { var sandbox1 = new AssemblySandbox(); var assetsFolder = Path.Combine(Directory.GetParent(GetType().Assembly.Location).FullName, "Assets"); sandbox1.LoadAssembly(Path.Combine(assetsFolder, "1", "ManagedLibrary.dll"), false); try { sandbox1.Reflection.InvokeStaticMethod("ManagedLibrary.Class1", "TestCallUnmanaged"); Assert.Fail(); } catch (DllNotFoundException) { } catch (Exception) { Assert.Fail(); } sandbox1.LoadAssembly(Path.Combine(assetsFolder, "2", "UnmanagedLibrary.dll"), false); var result = sandbox1.Reflection.InvokeStaticMethod("ManagedLibrary.Class1", "TestCallUnmanaged"); Assert.IsInstanceOfType(result, typeof(int)); Assert.AreEqual(123, (int)result); sandbox1.Dispose(); }
/// <summary> /// Compiles the program in memory and load it (to use it later) or returns the build errors. /// </summary> /// <param name="callback">The cross-AppDomain task proxy.</param> /// <returns>Returns the build errors, or null if it succeed.</returns> internal void Build(MarshaledResultSetter <AggregateException> callback) { Requires.NotNull(_middleware, nameof(_middleware)); if (DebugMode) { throw new UnexpectedException(new Exception(L.BaZic.Runtime.BaZicInterpreter.CannotBuildAfterStartDebug)); } CheckState(BaZicInterpreterState.Ready, BaZicInterpreterState.Stopped, BaZicInterpreterState.StoppedWithError); if (ProgramIsOptimized) { throw new InvalidOperationException(L.BaZic.Runtime.BaZicInterpreter.CannotRunOptimizedProgramInRelease); } Error = null; ChangeState(this, new BaZicInterpreterStateChangeEventArgs(BaZicInterpreterState.Running)); AggregateException buildErrors = null; var currentCulture = LocalizationHelper.GetCurrentCulture(); _mainInterpreterTask = Task.Run(() => { LocalizationHelper.SetCurrentCulture(currentCulture, false, false); _compilerResult = Build(BaZicCompilerOutputType.DynamicallyLinkedLibrary, string.Empty, string.Empty, string.Empty, false); if (_compilerResult.BuildErrors != null) { buildErrors = _compilerResult.BuildErrors; ChangeState(this, new UnexpectedException(_compilerResult.BuildErrors)); } else { _assemblySandbox.LoadAssembly(_compilerResult.Assembly, false); _compilerResult.Dispose(); _releaseModeForced = true; ChangeState(this, new BaZicInterpreterStateChangeEventArgs(BaZicInterpreterState.Stopped)); } }); _mainInterpreterTask.ContinueWith((task) => { callback.SetResult(buildErrors); }); }
public void AssemblySandboxLoadType() { var sandbox1 = new AssemblySandbox(); try { Type.GetType("System.Windows.UIElement", true, false); Assert.Fail(); } catch (TypeLoadException) { } catch { Assert.Fail(); } try { sandbox1.GetTypeRef("System.Windows.UIElement"); Assert.Fail(); } catch (TypeLoadException) { } catch { Assert.Fail(); } sandbox1.LoadAssembly("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); try { Type.GetType("System.Windows.UIElement", true, false); Assert.Fail(); } catch (TypeLoadException) { } catch { Assert.Fail(); } Assert.AreEqual(1, sandbox1.GetAssemblies().Count); Assert.IsNotNull(sandbox1.GetTypeRef("System.Windows.UIElement", "PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")); Assert.AreEqual(1, sandbox1.GetAssemblies().Count); Assert.IsNotNull(sandbox1.GetTypeRef("System.Windows.UIElement")); Assert.AreEqual(6, sandbox1.GetAssemblies().Count); sandbox1.Dispose(); }
public void AssemblySandboxLoadFromPath() { var sandbox1 = new AssemblySandbox(); var sandbox2 = new AssemblySandbox(); Assert.AreEqual(0, sandbox1.GetAssemblies().Count); Assert.AreEqual(0, sandbox2.GetAssemblies().Count); sandbox1.LoadAssembly(@"C:\WINDOWS\Microsoft.Net\assembly\GAC_32\PresentationCore\v4.0_4.0.0.0__31bf3856ad364e35\PresentationCore.dll"); Assert.AreEqual("PresentationCore", sandbox1.GetAssemblies().Single().Name); Assert.AreEqual(0, sandbox2.GetAssemblies().Count); sandbox1.LoadAssembly("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); Assert.AreEqual("PresentationCore", sandbox1.GetAssemblies().Single().Name); Assert.AreEqual(0, sandbox2.GetAssemblies().Count); sandbox1.Dispose(); sandbox2.Dispose(); }
public void AssemblySandboxLoadFromFullName() { var sandbox1 = new AssemblySandbox(); var sandbox2 = new AssemblySandbox(); Assert.AreEqual(0, sandbox1.GetAssemblies().Count); Assert.AreEqual(0, sandbox2.GetAssemblies().Count); sandbox1.LoadAssembly("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); Assert.AreEqual("PresentationCore", sandbox1.GetAssemblies().Single().Name); Assert.AreEqual(0, sandbox2.GetAssemblies().Count); sandbox1.LoadAssembly("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); Assert.AreEqual("PresentationCore", sandbox1.GetAssemblies().Single().Name); Assert.AreEqual(0, sandbox2.GetAssemblies().Count); sandbox1.Dispose(); sandbox2.Dispose(); }