DiscoStu
DiscoStu

Reputation: 101

Activator.CreateInstance throws Exception when references Type moved to another assembly

I am in the process of refactoring a Legacy Application and want to keep the Plugin System compatible, while getting some issues out of the way.

The problem is, that I needed to move the Interface used for extensions to a different assembly. While doing that, I kept the namespace intact.

In AssemblyB:

namespace OldNamespace;
public interface IPlugin
{
  void DoSomething();
}

Out at our customers are multiple Plugins, that use this interface, but back when they were developed it was still in AssemblyA like this in CustomAssembly:

using OldNamespace;

namespace CustomPlugin;

public class Plugin : IPlugin
{
  // ...
}

So the idea is, that this code can stay the same.

My challenge now is, that I want to load the existing Plugins in the Updated application with Activator.CreateInstance("CustomAssembly", "Plugin"); But if I try this, I get the Exception "Could not load type 'IPlugin' from assembly 'AssemblyA'

I don't really understand why it is searched for explicitly in AssemblyA. I never specified in my code, that it needs to load IPlugin from the old Assembly. The namespace is the same and both Assemblies (A and B) are loaded in the application. Can I somehow control how the Type-Loading for IPlugin is performed? I experimented a bit with the event AppDomain.TypeResolve but it wasn't even called.

An option of course is to newly compile the old Plugins, but there aresome, where there is no source code available anymore.

Upvotes: 0

Views: 66

Answers (0)

Related Questions