Skip to content

Commit 74402f8

Browse files
committed
Simplify loading assemblies
Use a HashSet instead of a Dictionary keyed by the assembly full name. If an assembly is loaded twice with the same AssemblyName it will be the same instance so a HashSet (without even a custom IEqualityComparer<Assembly>) is the perfect solution. Also, Assembly.Load can throw many exceptions but won't return null.
1 parent 92d37fc commit 74402f8

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ internal static IConfigurationArgumentValue GetArgumentValue(IConfigurationSecti
359359
static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfiguration section, AssemblyFinder assemblyFinder)
360360
{
361361
var serilogAssembly = typeof(ILogger).Assembly;
362-
var assemblies = new Dictionary<string, Assembly> { [serilogAssembly.FullName] = serilogAssembly };
362+
var assemblies = new HashSet<Assembly> { serilogAssembly };
363363

364364
var usingSection = section.GetSection("Using");
365365
if (usingSection.GetChildren().Any())
@@ -371,16 +371,14 @@ static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfiguration
371371
$"A zero-length or whitespace assembly name was supplied to a {usingSection.Path} configuration statement.");
372372

373373
var assembly = Assembly.Load(new AssemblyName(simpleName));
374-
if (!assemblies.ContainsKey(assembly.FullName))
375-
assemblies.Add(assembly.FullName, assembly);
374+
assemblies.Add(assembly);
376375
}
377376
}
378377

379378
foreach (var assemblyName in assemblyFinder.FindAssembliesContainingName("serilog"))
380379
{
381380
var assumed = Assembly.Load(assemblyName);
382-
if (assumed != null && !assemblies.ContainsKey(assumed.FullName))
383-
assemblies.Add(assumed.FullName, assumed);
381+
assemblies.Add(assumed);
384382
}
385383

386384
if (assemblies.Count == 1)
@@ -395,7 +393,7 @@ This is most likely because the application is published as single-file.
395393
throw new InvalidOperationException(message);
396394
}
397395

398-
return assemblies.Values;
396+
return assemblies;
399397
}
400398

401399
void CallConfigurationMethods(ILookup<string, Dictionary<string, IConfigurationArgumentValue>> methods, IReadOnlyCollection<MethodInfo> configurationMethods, object receiver)

0 commit comments

Comments
 (0)