From 8a88a18ef23330c513fcfad986ece7b7b4ed7318 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Tue, 21 Aug 2018 15:55:25 +0200 Subject: [PATCH 1/2] Use GetExportedTypes where possible and filter nested types This is basically what @dmitriyse prepared in PR 528 without the event code that I don't understand. --- CHANGELOG.md | 2 ++ src/runtime/assemblymanager.cs | 17 +++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dc668b0c..00f70e014 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. ## [unreleased][] ### Added + - Added support for embedding python into dotnet core 2.0 (NetStandard 2.0) - Added new build system (pythonnet.15.sln) based on dotnetcore-sdk/xplat(crossplatform msbuild). Currently there two side-by-side build systems that produces the same output (net40) from the same sources. @@ -36,6 +37,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. - Fixed errors breaking .NET Remoting on method invoke ([#276][i276]) - Fixed PyObject.GetHashCode ([#676][i676]) - Fix memory leaks due to spurious handle incrementation ([#691][i691]) +- Fix spurious assembly loading exceptions from private types ([#703][i703]) ## [2.3.0][] - 2017-03-11 diff --git a/src/runtime/assemblymanager.cs b/src/runtime/assemblymanager.cs index d63930a58..1f64a0594 100644 --- a/src/runtime/assemblymanager.cs +++ b/src/runtime/assemblymanager.cs @@ -343,9 +343,7 @@ internal static void ScanAssembly(Assembly assembly) // A couple of things we want to do here: first, we want to // gather a list of all of the namespaces contributed to by // the assembly. - - Type[] types = assembly.GetTypes(); - foreach (Type t in types) + foreach (Type t in GetTypes(assembly)) { string ns = t.Namespace ?? ""; if (!namespaces.ContainsKey(ns)) @@ -419,10 +417,9 @@ public static List GetNames(string nsname) { foreach (Assembly a in namespaces[nsname].Keys) { - Type[] types = a.GetTypes(); - foreach (Type t in types) + foreach (Type t in GetTypes(a)) { - if ((t.Namespace ?? "") == nsname) + if ((t.Namespace ?? "") == nsname && !t.IsNested) { names.Add(t.Name); } @@ -461,5 +458,13 @@ public static Type LookupType(string qname) } return null; } + + internal static Type[] GetTypes(Assembly a) + { + if (a.IsDynamic) + return a.GetTypes(); + else + return a.GetExportedTypes(); + } } } From 76314725be60ca0e49a39dcbc4405ae7c7281097 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Wed, 14 Nov 2018 12:30:28 +0100 Subject: [PATCH 2/2] Catch exceptions during type loading --- src/runtime/assemblymanager.cs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/runtime/assemblymanager.cs b/src/runtime/assemblymanager.cs index e07802518..66c4b9904 100644 --- a/src/runtime/assemblymanager.cs +++ b/src/runtime/assemblymanager.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Reflection; using System.Threading; @@ -470,9 +471,28 @@ public static Type LookupType(string qname) internal static Type[] GetTypes(Assembly a) { if (a.IsDynamic) - return a.GetTypes(); + { + try + { + return a.GetTypes(); + } + catch (ReflectionTypeLoadException exc) + { + // Return all types that were successfully loaded + return exc.Types.Where(x => x != null).ToArray(); + } + } else - return a.GetExportedTypes(); + { + try + { + return a.GetExportedTypes(); + } + catch (FileNotFoundException) + { + return new Type[0]; + } + } } } -} +} \ No newline at end of file