Skip to content

Commit

Permalink
feat: Use ConcurrentDictionary type to provide thread safety (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 committed May 25, 2024
1 parent f02e115 commit 57b82f1
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ This library contains these features:
## Limitations

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

## Why did I create this library?
Expand Down
6 changes: 3 additions & 3 deletions src/Core/PluginLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand All @@ -10,7 +11,7 @@ namespace CPlugin.Net;
/// </summary>
public static class PluginLoader
{
private readonly static Dictionary<string, Assembly> s_assemblies = new();
private readonly static ConcurrentDictionary<string, Assembly> s_assemblies = new();

/// <summary>
/// Gets the plugin assemblies.
Expand All @@ -32,7 +33,6 @@ public static class PluginLoader
public static void Load(CPluginConfigurationBase configuration)
{
ArgumentNullException.ThrowIfNull(configuration);

var assemblyFiles = configuration.GetPluginFiles();
foreach (string assemblyFile in assemblyFiles)
{
Expand All @@ -47,7 +47,7 @@ private static void LoadAssembly(string assemblyFile)
var loadContext = new PluginLoadContext(assemblyFile);
var assemblyName = AssemblyName.GetAssemblyName(assemblyFile);
var currentAssembly = loadContext.LoadFromAssemblyName(assemblyName);
s_assemblies.Add(assemblyFile, currentAssembly);
s_assemblies.TryAdd(assemblyFile, currentAssembly);
PluginLogger.DefaultLogInformation(currentAssembly.GetName().Name, currentAssembly.FullName);
}

Expand Down

0 comments on commit 57b82f1

Please sign in to comment.