Skip to content

Commit 5c9f035

Browse files
authored
Use GetExportedTypes where possible and filter nested types (pythonnet#723)
* Use GetExportedTypes where possible and filter nested types * Catch exceptions during type loading This is basically what @dmitriyse prepared in PR 528 without the event code that I don't understand.
1 parent 088580d commit 5c9f035

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
88
## [unreleased][]
99

1010
### Added
11+
1112
- Added support for embedding python into dotnet core 2.0 (NetStandard 2.0)
1213
- Added new build system (pythonnet.15.sln) based on dotnetcore-sdk/xplat(crossplatform msbuild).
1314
Currently there two side-by-side build systems that produces the same output (net40) from the same sources.
@@ -46,6 +47,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
4647
- Fixed errors breaking .NET Remoting on method invoke ([#276][i276])
4748
- Fixed PyObject.GetHashCode ([#676][i676])
4849
- Fix memory leaks due to spurious handle incrementation ([#691][i691])
50+
- Fix spurious assembly loading exceptions from private types ([#703][i703])
4951
- Fix inheritance of non-abstract base methods ([#755][i755])
5052

5153

src/runtime/assemblymanager.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.IO;
7+
using System.Linq;
78
using System.Reflection;
89
using System.Threading;
910

@@ -348,9 +349,7 @@ internal static void ScanAssembly(Assembly assembly)
348349
// A couple of things we want to do here: first, we want to
349350
// gather a list of all of the namespaces contributed to by
350351
// the assembly.
351-
352-
Type[] types = assembly.GetTypes();
353-
foreach (Type t in types)
352+
foreach (Type t in GetTypes(assembly))
354353
{
355354
string ns = t.Namespace ?? "";
356355
if (!namespaces.ContainsKey(ns))
@@ -424,10 +423,9 @@ public static List<string> GetNames(string nsname)
424423
{
425424
foreach (Assembly a in namespaces[nsname].Keys)
426425
{
427-
Type[] types = a.GetTypes();
428-
foreach (Type t in types)
426+
foreach (Type t in GetTypes(a))
429427
{
430-
if ((t.Namespace ?? "") == nsname)
428+
if ((t.Namespace ?? "") == nsname && !t.IsNested)
431429
{
432430
names.Add(t.Name);
433431
}
@@ -466,5 +464,32 @@ public static Type LookupType(string qname)
466464
}
467465
return null;
468466
}
467+
468+
internal static Type[] GetTypes(Assembly a)
469+
{
470+
if (a.IsDynamic)
471+
{
472+
try
473+
{
474+
return a.GetTypes();
475+
}
476+
catch (ReflectionTypeLoadException exc)
477+
{
478+
// Return all types that were successfully loaded
479+
return exc.Types.Where(x => x != null).ToArray();
480+
}
481+
}
482+
else
483+
{
484+
try
485+
{
486+
return a.GetExportedTypes();
487+
}
488+
catch (FileNotFoundException)
489+
{
490+
return new Type[0];
491+
}
492+
}
493+
}
469494
}
470-
}
495+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy