Fix TypeLoader (#1320)

* no ilmerge

* .

* .

* nullable

* .UsingNuGet

* fix

* .

* directoriesToSearch

* .
This commit is contained in:
Stef Heyenrath
2025-06-15 11:44:09 +02:00
committed by GitHub
parent 70a9180af4
commit 7b93b2668d
12 changed files with 323 additions and 26 deletions

View File

@@ -28,7 +28,7 @@ internal class MimeKitUtils : IMimeKitUtils
if (requestMessage.BodyData != null &&
requestMessage.Headers?.TryGetValue(HttpKnownHeaderNames.ContentType, out var contentTypeHeader) == true &&
StartsWithMultiPart(contentTypeHeader) // Only parse when "multipart/mixed"
StartsWithMultiPart(contentTypeHeader)
)
{
var bytes = requestMessage.BodyData?.DetectedBodyType switch

View File

@@ -36,9 +36,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PolySharp" Version="1.15.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Stef.Validation" Version="0.1.1" />
</ItemGroup>

View File

@@ -51,7 +51,7 @@ internal static class TypeLoader
{
var key = typeof(TInterface).FullName!;
var pluginType = Assemblies.GetOrAdd(key, _ =>
return Assemblies.GetOrAdd(key, _ =>
{
if (TryFindTypeInDlls<TInterface>(null, out var foundType))
{
@@ -60,7 +60,6 @@ internal static class TypeLoader
throw new DllNotFoundException($"No dll found which implements interface '{key}'.");
});
return pluginType;
}
private static Type GetPluginTypeByFullName<TInterface>(string implementationTypeFullName) where TInterface : class
@@ -68,7 +67,7 @@ internal static class TypeLoader
var @interface = typeof(TInterface).FullName;
var key = $"{@interface}_{implementationTypeFullName}";
var pluginType = Assemblies.GetOrAdd(key, _ =>
return Assemblies.GetOrAdd(key, _ =>
{
if (TryFindTypeInDlls<TInterface>(implementationTypeFullName, out var foundType))
{
@@ -77,28 +76,40 @@ internal static class TypeLoader
throw new DllNotFoundException($"No dll found which implements Interface '{@interface}' and has FullName '{implementationTypeFullName}'.");
});
return pluginType;
}
private static bool TryFindTypeInDlls<TInterface>(string? implementationTypeFullName, [NotNullWhen(true)] out Type? pluginType) where TInterface : class
{
foreach (var file in Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dll"))
#if NETSTANDARD1_3
var directoriesToSearch = new[] { AppContext.BaseDirectory };
#else
var processDirectory = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName);
var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var directoriesToSearch = new[] { processDirectory, assemblyDirectory }
.Where(d => !string.IsNullOrEmpty(d))
.Distinct()
.ToArray();
#endif
foreach (var directory in directoriesToSearch)
{
try
foreach (var file in Directory.GetFiles(directory!, "*.dll"))
{
var assembly = Assembly.Load(new AssemblyName
try
{
Name = Path.GetFileNameWithoutExtension(file)
});
var assembly = Assembly.Load(new AssemblyName
{
Name = Path.GetFileNameWithoutExtension(file)
});
if (TryGetImplementationTypeByInterfaceAndOptionalFullName<TInterface>(assembly, implementationTypeFullName, out pluginType))
{
return true;
if (TryGetImplementationTypeByInterfaceAndOptionalFullName<TInterface>(assembly, implementationTypeFullName, out pluginType))
{
return true;
}
}
catch
{
// no-op: just try next .dll
}
}
catch
{
// no-op: just try next .dll
}
}

View File

@@ -65,7 +65,7 @@
<PackageReference Include="JmesPath.Net" Version="1.0.330" />
<PackageReference Include="AnyOf" Version="0.4.0" />
<PackageReference Include="TinyMapper.Signed" Version="4.0.0" />
<PackageReference Include="PolySharp" Version="1.15.0">
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>