natemcmaster / DotNetCorePlugins

.NET Core library for dynamically loading code
Apache License 2.0
1.58k stars 227 forks source link

[Question] Fody Weaving impact on Plugins and not finding types. #245

Closed Simonl9l closed 2 years ago

Simonl9l commented 2 years ago

We're quite invested in using the Plugin Loader, its been great thanks for making this available!

Problem:

We have a solution we're now trying to package/distribute. Some of our plugins have a significant number of dependent runtime assemblies. Given this our plan was to leverage Fody to IL merge each entire plugin down to a single binary.

Understandably this messes with Type's in the plugin such that when we try and load it via PluginLoader.CreateFromAssemblyFile whist it loads successfully supplying the plugin type (from a shared assembly) as the host understand it, it then appears (obviously) to no longer support the given plugin interface Type as the implementation assembly is lost in the IL weaving process.

We get null back from:

 var supportedType = assembly
     .GetTypes()
     .FirstOrDefault(t => pluginType.IsAssignableFrom(t) && !t.IsAbstract);

Solution Approach:

Since we know the path to the plugin etc. and assume we can load the assembly and emulate its types looking for our plugin interface by name. If found we also have the assumption that there may be some mechanism to evaluate that the interfaces signature is identical and its safe to case between them.

I see a couple of other issue mentioning Fody, but none seem to talk to this specifically, do you have a recommendations on how to get this to work.

natemcmaster commented 2 years ago

I believe tools like Fody or ILMerge fundamentally rewrite your application code after it passes through the C# compiler...including changing type identities in weird ways. This is not something I believe the plugins library will be able to workaround.