2

I have C# application that load plugins dll and run these. All plugin inherit from BasePlugin class. My application instantiate plugins as BasePlugin and work with these. For example get the name of plugin, call functions and other. Exist an Interface that plugin can call API from my application and interaction with my application.

My BasePlugin class:

public abstract class BasePlugin{
    public abstract string Name {get;}
    public abstract int A {get;set;}
    public abstract void Do(IAPI api);     
}

IAPI Interface:

public interface IAPI
{
    void F1();
    int F2(int a);
}

I put IAPI and BasePlugin in .net library project and my application and all plugins use this library. In my project get Directory of plugins and load plugin with this code:

    public List<BasePlugin> GetAllPlugins(string dir)
    {
        List<BasePlugin> plugins = new List<BasePlugin>();
        foreach (string dll in Directory.GetFiles(dir, "*.dll"))
        {
            Assembly loadedAssembly = Assembly.LoadFrom(dll);
            List<Type> types = loadedAssembly.GetTypes().Where(t => t.IsSubclassOf(typeof(BasePlugin))).ToList();
            foreach (Type t in types)
            {
                plugins.Add((BasePlugin)Activator.CreateInstance(t));
            }
        }
        return plugins;
    }    

I develop C# plugin easy. But how can I develop C++, Python, Java, matlab, ... plugin for my appliaction?

sharafi
  • 531
  • 1
  • 7
  • 19
  • Making plugins from native libraries boils down to loading native library and routing everything manually. First step: how to load library - [click](https://stackoverflow.com/q/11425202/1997232) (look for better questions/answers), second step: routing methods - [click](https://stackoverflow.com/a/2818948/1997232).. – Sinatr May 05 '20 at 13:07
  • Write your plugin in c# then have the plugin call a C++ dll. You can also use c++-cli but its often simpler to just use the marshalling interfaces with a C API. – Alan Birtles May 05 '20 at 13:10

1 Answers1

0

For c++ you can write your plugin in c++/cli. This will produce a managed dll, just as if the plugin was written in c#.

For Python there is Iron Python that runs in the .Net runtime and should also be fairly easy to write a plugin in.

For other languages, like Java & matlab, there is no easy way. Each language require its own runtime and environment, so they would more or less have to run in separate processes and use some IPC mechanism to interface with eachother.

JonasH
  • 28,608
  • 2
  • 10
  • 23