Skip to content

Commit 57b82f1

Browse files
authored
feat: Use ConcurrentDictionary type to provide thread safety (#37)
1 parent f02e115 commit 57b82f1

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ This library contains these features:
6060
## Limitations
6161

6262
This library contains these limitations:
63-
- The plugin loader is not thread-safe.
6463
- There is no support for unload plugins.
6564

6665
## Why did I create this library?

src/Core/PluginLoader.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Reflection;
@@ -10,7 +11,7 @@ namespace CPlugin.Net;
1011
/// </summary>
1112
public static class PluginLoader
1213
{
13-
private readonly static Dictionary<string, Assembly> s_assemblies = new();
14+
private readonly static ConcurrentDictionary<string, Assembly> s_assemblies = new();
1415

1516
/// <summary>
1617
/// Gets the plugin assemblies.
@@ -32,7 +33,6 @@ public static class PluginLoader
3233
public static void Load(CPluginConfigurationBase configuration)
3334
{
3435
ArgumentNullException.ThrowIfNull(configuration);
35-
3636
var assemblyFiles = configuration.GetPluginFiles();
3737
foreach (string assemblyFile in assemblyFiles)
3838
{
@@ -47,7 +47,7 @@ private static void LoadAssembly(string assemblyFile)
4747
var loadContext = new PluginLoadContext(assemblyFile);
4848
var assemblyName = AssemblyName.GetAssemblyName(assemblyFile);
4949
var currentAssembly = loadContext.LoadFromAssemblyName(assemblyName);
50-
s_assemblies.Add(assemblyFile, currentAssembly);
50+
s_assemblies.TryAdd(assemblyFile, currentAssembly);
5151
PluginLogger.DefaultLogInformation(currentAssembly.GetName().Name, currentAssembly.FullName);
5252
}
5353

0 commit comments

Comments
 (0)