Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Reference the assembly loader in the Main method to stop it being opt…
…imized away, and don't reload assemblies once they're already loaded.
  • Loading branch information
tonyroberts committed Apr 11, 2014
commit 10a2f8f661bcf616bce5f132db8603dd4b235a13
16 changes: 15 additions & 1 deletion pythonnet/src/console/pythonconsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using System;
using System.Reflection;
using System.Collections.Generic;
using Python.Runtime;

namespace Python.Runtime {
Expand All @@ -19,6 +20,9 @@ private PythonConsole() {}

[STAThread]
public static int Main(string[] args) {
// reference the static assemblyLoader to stop it being optimized away
AssemblyLoader a = assemblyLoader;

string [] cmd = Environment.GetCommandLineArgs();
PythonEngine.Initialize();

Expand All @@ -31,16 +35,26 @@ public static int Main(string[] args) {
// Register a callback function to load embedded assmeblies.
// (Python.Runtime.dll is included as a resource)
private sealed class AssemblyLoader {
Dictionary<string, Assembly> loadedAssemblies;

public AssemblyLoader() {
loadedAssemblies = new Dictionary<string, Assembly>();

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
String resourceName = new AssemblyName(args.Name).Name + ".dll";

if (loadedAssemblies.ContainsKey(resourceName)) {
return loadedAssemblies[resourceName];
}

// looks for the assembly from the resources and load it
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
if (stream != null) {
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
Assembly assembly = Assembly.Load(assemblyData);
loadedAssemblies[resourceName] = assembly;
return assembly;
}
}

Expand Down