Skip to content

Commit b98e589

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. This was initially introduced in 74402f8 but merged incorrectly in ac1b8c8.
1 parent 325577e commit b98e589

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
@@ -360,7 +360,7 @@ internal static IConfigurationArgumentValue GetArgumentValue(IConfigurationSecti
360360
static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfiguration section, AssemblyFinder assemblyFinder)
361361
{
362362
var serilogAssembly = typeof(ILogger).Assembly;
363-
var assemblies = new Dictionary<string, Assembly> { [serilogAssembly.FullName!] = serilogAssembly };
363+
var assemblies = new HashSet<Assembly> { serilogAssembly };
364364

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

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

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

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

399-
return assemblies.Values;
397+
return assemblies;
400398
}
401399

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

0 commit comments

Comments
 (0)