mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-17 08:56:51 +01:00
* matcher
* wip
* fix
* <VersionPrefix>1.4.0</VersionPrefix>
* .
* x
* ?
* netstandard2.1
* {}
* test
* Fix: Assembly with same name is already loaded
* _format file
* AssemblyFile = $"WireMock.CodeHelper.Class{Guid.NewGuid()}"
* AssemblyFile = $"WireMock.CodeHelper.Class{Guid.NewGuid().ToString().Replace("-", "")}"
* GC
* x
* remove load ex
* ret
* readme
* no GC
* GetImplementationTypeByInterface
* ``
* PluginLoader
* type
57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace WireMock.Plugin
|
|
{
|
|
internal static class PluginLoader
|
|
{
|
|
private static readonly ConcurrentDictionary<Type, Type> Assemblies = new ConcurrentDictionary<Type, Type>();
|
|
|
|
public static T Load<T>(params object[] args) where T : class
|
|
{
|
|
var foundType = Assemblies.GetOrAdd(typeof(T), (type) =>
|
|
{
|
|
var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dll");
|
|
|
|
Type pluginType = null;
|
|
foreach (var file in files)
|
|
{
|
|
try
|
|
{
|
|
var assembly = Assembly.Load(new AssemblyName
|
|
{
|
|
Name = Path.GetFileNameWithoutExtension(file)
|
|
});
|
|
|
|
pluginType = GetImplementationTypeByInterface<T>(assembly);
|
|
if (pluginType != null)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// no-op: just try next .dll
|
|
}
|
|
}
|
|
|
|
if (pluginType != null)
|
|
{
|
|
return pluginType;
|
|
}
|
|
|
|
throw new DllNotFoundException($"No dll found which implements type '{type}'");
|
|
});
|
|
|
|
return (T)Activator.CreateInstance(foundType, args);
|
|
}
|
|
|
|
private static Type GetImplementationTypeByInterface<T>(Assembly assembly)
|
|
{
|
|
return assembly.GetTypes().FirstOrDefault(t => typeof(T).IsAssignableFrom(t) && !t.GetTypeInfo().IsInterface);
|
|
}
|
|
}
|
|
} |