Question

I have 4 classes; A, B, C and D

I want to load these classes dynamically based on some criterion

These classes are in their own C# Class Library (dll) project. These classes make use of Helix3D Toolkit.

  1. How can I design a plugin system that reads all the .dll files in a plugin folder, and allow for them to be somehow labeled or numbered (could a static value do this?) to be called when needed?

  2. If I reference the A class in a dll while the A class is in the main project, will the dll in the plugins folder automatically find the A class the plugin's a class subclasses when dinamically loaded into the main app?

Was it helpful?

Solution

I edited your question because it was far too long and verbose (3000 chars).

Answer to Question 1

To load dlls dynamically, look at this answer. It uses reflection to iterate over every type in the assembly and uses Activator.CreateInstance() to instantiate the classes.

For your plugin system I would create something like:

In your .exe:

public interface IPlugin
{
    void DoSomething();
}

public class A : IPlugin
{
    public virtual void DoSomething() {}
}

public class B : IPlugin
{
    public virtual void DoSomething() {}
}

public class C : IPlugin
{
    public virtual void DoSomething() {}
}

public class D : IPlugin
{
    public virtual void DoSomething() {}
}

In a Dll:

public class NewA : A
{
    public override void DoSomething() { base.DoSomething() }
}

public class NewB : B
{
    public override void DoSomething() { base.DoSomething() }
}

public class NewC : C
{
    public override void DoSomething() { base.DoSomething() }
}

public class NewD : D
{
    public override void DoSomething() { base.DoSomething() }
}

In your client code, code against IPlugin. Your client code does not know about the concrete classes A, B, C, D.

Load the classes that you want from the dlls you have and inject them into IPlugin

Answer to Question 2

You can have classes with the same class name as long as they are in separate namespaces, you cannot have classes with the same name in the same namespace. In your case, they ought to be in different namespaces so you are okay.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top