'Why dynamically loaded .NET 6 plugins are no longer IsAssignableFrom the plugin interface when publishing with trim unused assemblies turned on?
I have created a small .NET 6 console application (GPU Offers on GitHub), which is using a plugin system to dynamically (at runtime) load plugins (DLL files).
When I dotnet publish the console app (not the plugins) with the option to trim unused assemblies, the plugins don't get recognized as a plugin by the console app anymore...
private static IEnumerable<IPluginInterface> CreatePlugins(Assembly assembly)
{
int count = 0;
foreach (Type type in assembly.GetTypes())
{
if (typeof(IPluginInterface).IsAssignableFrom(type))
{
IPluginInterface result = Activator.CreateInstance(type) as IPluginInterface;
if (result != null)
{
count++;
yield return result;
}
}
}
if (count == 0)
{
string availableTypes = string.Join(", ", Enumerable.Select(assembly.GetTypes(), t => t.FullName));
throw new ApplicationException(
$"Can't find any type which implements IPluginInterface in {assembly} from {assembly.Location}.\nAvailable types: {availableTypes}");
}
}
When I disable the trim unused assemblies option for the console application, the plugin is recognized again.
In my thought, the IPluginInterface cannot be treated as an "unused assembly", as it is actively used in the console application, as you can see above.
So, why the console application does not recognize the plugins anymore, when the trim unused assemblies publish option is enabled?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
