47 lines
1.6 KiB
C#
47 lines
1.6 KiB
C#
using System.Reflection;
|
|
|
|
namespace SushiBarContracts.DI;
|
|
|
|
public static partial class ServiceProviderLoader
|
|
{
|
|
public static IImplementationExtension? GetImplementationExtensions()
|
|
{
|
|
IImplementationExtension? source = null;
|
|
var files =
|
|
Directory.GetFiles(TryGetImplementationExtensionsFolder(), "*.dll",
|
|
SearchOption.AllDirectories);
|
|
foreach (var file in files.Distinct())
|
|
{
|
|
var asm = Assembly.LoadFrom(file);
|
|
foreach (var t in asm.GetExportedTypes())
|
|
{
|
|
if (!t.IsClass || !typeof(IImplementationExtension).IsAssignableFrom(t)) continue;
|
|
if (source == null)
|
|
{
|
|
source = (IImplementationExtension)Activator.CreateInstance(t)!;
|
|
}
|
|
else
|
|
{
|
|
var newSource = (IImplementationExtension)Activator.CreateInstance(t)!;
|
|
if (newSource.Priority > source.Priority)
|
|
{
|
|
source = newSource;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return source;
|
|
}
|
|
private static string TryGetImplementationExtensionsFolder()
|
|
{
|
|
var directory = new
|
|
DirectoryInfo(Directory.GetCurrentDirectory());
|
|
while (directory != null && directory
|
|
.GetDirectories("ImplementationExtensions", SearchOption.AllDirectories)
|
|
.All(x => x.Name != "ImplementationExtensions"))
|
|
{
|
|
directory = directory.Parent;
|
|
}
|
|
return $"{directory?.FullName}\\ImplementationExtensions";
|
|
}
|
|
} |