From 0197a3eaf5593aef30edc2aca01b59d37c31dc22 Mon Sep 17 00:00:00 2001 From: Lucas Trzesniewski Date: Thu, 13 Jun 2024 11:33:25 +0200 Subject: [PATCH] Use Assembly.GetTypes() safely --- src/ZeroLog.Impl.Full/LogManager.Impl.cs | 3 ++- src/ZeroLog.Impl.Full/Support/TypeUtil.cs | 18 ++++++++++++++++++ src/ZeroLog.sln.DotSettings | 4 ++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/ZeroLog.Impl.Full/LogManager.Impl.cs b/src/ZeroLog.Impl.Full/LogManager.Impl.cs index 909b5c2..a2afba6 100644 --- a/src/ZeroLog.Impl.Full/LogManager.Impl.cs +++ b/src/ZeroLog.Impl.Full/LogManager.Impl.cs @@ -3,6 +3,7 @@ using System.Reflection; using System.Threading; using ZeroLog.Configuration; +using ZeroLog.Support; namespace ZeroLog; @@ -142,7 +143,7 @@ public static void RegisterAllEnumsFrom(Assembly assembly) if (assembly == null) throw new ArgumentNullException(nameof(assembly)); - foreach (var type in assembly.GetTypes().Where(t => t.IsEnum)) + foreach (var type in TypeUtil.GetLoadableTypes(assembly).Where(t => t.IsEnum)) RegisterEnum(type); } diff --git a/src/ZeroLog.Impl.Full/Support/TypeUtil.cs b/src/ZeroLog.Impl.Full/Support/TypeUtil.cs index 0e767c3..b81b2aa 100644 --- a/src/ZeroLog.Impl.Full/Support/TypeUtil.cs +++ b/src/ZeroLog.Impl.Full/Support/TypeUtil.cs @@ -1,5 +1,7 @@ using System; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using static System.Linq.Expressions.Expression; @@ -41,6 +43,22 @@ public static bool GetIsUnmanagedSlow(Type type) .MakeGenericMethod(type) .Invoke(null, null)!; } + + /// + /// Gets the types defined in the given assembly, except those which could not be loaded. + /// + [DebuggerStepThrough] + public static Type[] GetLoadableTypes(Assembly assembly) + { + try + { + return assembly.GetTypes(); + } + catch (ReflectionTypeLoadException ex) + { + return ex.Types.Where(t => t is not null).ToArray()!; + } + } } internal static class TypeUtil diff --git a/src/ZeroLog.sln.DotSettings b/src/ZeroLog.sln.DotSettings index 081bd3d..d459ebb 100644 --- a/src/ZeroLog.sln.DotSettings +++ b/src/ZeroLog.sln.DotSettings @@ -19,6 +19,9 @@ <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> <Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /> <Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /> + <Policy><Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static readonly fields (private)"><ElementKinds><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /></Policy> + <Policy><Descriptor Staticness="Any" AccessRightKinds="Private" Description="Constant fields (private)"><ElementKinds><Kind Name="CONSTANT_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /></Policy> + <Policy><Descriptor Staticness="Any" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Constant fields (not private)"><ElementKinds><Kind Name="CONSTANT_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></Policy> <Policy><Descriptor Staticness="Static, Instance" AccessRightKinds="Protected" Description="Protected fields"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /><Kind Name="CONSTANT_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /></Policy> C:\Users\Lucas\AppData\Local\JetBrains\Transient\ReSharperPlatformVs15\v12_9acb2574\SolutionCaches @@ -31,6 +34,7 @@ True True True + True True True True \ No newline at end of file