0

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?

Sam Leach
  • 12,746
  • 9
  • 45
  • 73
JasonX
  • 503
  • 7
  • 21
  • Thank you Sam for the edit, but you sort of missed the point of my question. I have class templates A, B, C and D, that I want to subclass in my plugins. And these classes are NOT in their own dll project, but in the main exe project as they break if moved into a separate project (helixes static Overlay.SetCoordinate3D has a bug). – JasonX Apr 03 '14 at 12:17
  • They should not break if you move them to their own assembly, are you referencing the helix dll(s) in the assembly you move them to? Also, define break. – Sam Leach Apr 03 '14 at 12:19
  • Right, sorry about that. Overlay.SetCooridnates3D(Vector3f position) sets a canvas child's coordinates relative to the underlying Helix3DViewport - a subclass of the WPF3D viewport I think. How this magic is done I have no clue, but it works fine if the class is in the main project. Once moved to a separate project and referenced, the same class sets the canvas coordinates to 0,0, no matter the Vector3 input. And yes, the Helix3Dtoolkit is referenced in both projects. I'm guessing it has something to do with access to the Helix3DViewport, but that's just a guess. – JasonX Apr 03 '14 at 12:27
  • Adding info here, if the base class is in the main project and is subclassed in a separate project, the overlay still works. Only when the base class is moved, the Overlay is acting out. – JasonX Apr 03 '14 at 12:34

1 Answers1

1

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.

Community
  • 1
  • 1
Sam Leach
  • 12,746
  • 9
  • 45
  • 73
  • I see, scrap the current in-main-app templates and replace them with IPlugin. But the A, B, C and D classes are HUGE, referencing several libraries each. With a few hundred planned, the size of it would be gigantic (relatively). Is there no way to only sublass and extend a method in a plugin? Edit: Never mind, I misread your answer. – JasonX Apr 03 '14 at 12:33