Creating an instance of a project class that we have no reference to using CreateInstance

197 Views Asked by At

I have 1 solution. With 3 project: Client, Visualisation and ClientInterface.

Project Client and Visualisation have reference to ClientInterface.

ClientInterface have class:

public abstract class VisualisationCollector
{        
    public abstract void Initialize();
}

Client:

public class Collector : VisualisationCollector
{
    public int Index {get; set;}
    protected override void Initialize(){...}
}

I am copy .dll from Client and past in Visualisation path:

AppDomain.CurrentDomain.BaseDirectory + "\Devices"

And now I am trying in Visualisation project:

var Collector = Activator.CreateInstance("Client", "Client.Collector").Unwrap();

I see only properties (but if there is a property of the class type, I can only see its name) from Collector without method. But I dont see method and properties from VisualisationCollector. And cant cast it on (VisualisationCollector) becouse throw:

'Unable to cast object of type' Client.Collector 'to type' ClientInterface.VisualisationCollector '.'

It work when I have reference in Visualisation to Client. But i cant do that.

Edit:

Currently I have achieved what I wanted in this way. But I don't like downloading this assembly a second time (because before that I have already loaded this library with assembly). But I don't know how to get this type by name.

Assembly assembly = AppDomain.CurrentDomain.Load(File.ReadAllBytes("Devices\\Client.dll"));
var form = assembly.CreateInstance("Client.Collector").GetType();
var assembly1 = Assembly.GetAssembly(form);
var vis = (VisualisationCollector)assembly1.CreateInstance("Client.Collector");
0

There are 0 best solutions below