예제 #1
0
        public static Assembly AddReference(string name)
        {
            AssemblyManager.UpdatePath();
            var      origNs   = AssemblyManager.GetNamespaces();
            Assembly assembly = null;

            assembly = AssemblyManager.FindLoadedAssembly(name);
            if (assembly == null)
            {
                assembly = AssemblyManager.LoadAssemblyPath(name);
            }
            if (assembly == null && AssemblyManager.TryParseAssemblyName(name) is { } parsedName)
            {
                assembly = AssemblyManager.LoadAssembly(parsedName);
            }
            if (assembly == null)
            {
                assembly = AssemblyManager.LoadAssemblyFullPath(name);
            }
            if (assembly == null)
            {
                throw new FileNotFoundException($"Unable to find assembly '{name}'.");
            }
            // Classes that are not in a namespace needs an extra nudge to be found.
            ImportHook.UpdateCLRModuleDict();

            // A bit heavyhanded, but we can't use the AssemblyManager's AssemblyLoadHandler
            // method because it may be called from other threads, leading to deadlocks
            // if it is called while Python code is executing.
            var currNs = AssemblyManager.GetNamespaces().Except(origNs);

            foreach (var ns in currNs)
            {
                ImportHook.AddNamespaceWithGIL(ns);
            }
            return(assembly);
        }