0

I have application created in WPF, one part of application is different for each client

At the momenet when I deploy my application to new client i must create new class or modified old becouse each client have diferent buisness logic.

For 2 client is no problem but there are more and more of them. I have idea to create plugins for my application. For example: I create one application (core), and copy only custom dll (plugin) to a specific folder on the disk

And here is my question, it's a good idea? I do not know if it will be efficient enough considering that one client may have few plugins.

My samples:

    interface IST
{
    string Name { get; set; }
    string WorkRequest(string connection);
    void Start();
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        var plcConnectionString = "";
        string[] allPlugins = Directory.GetFiles(@"D:\app_plugins\", "*.dll", SearchOption.AllDirectories);

        foreach (var item in allPlugins)
        {
            Assembly myassembly = Assembly.LoadFrom(item);
            Type type = myassembly.GetType("appPlugins.ST");

            object instance = Activator.CreateInstance(type);
            MethodInfo[] methods = type.GetMethods();
            object res = methods[0].Invoke(instance, new object[] { plcConnectionString }); // WorkRequest
        }
    }
}

All plugins implementation interface IST can I somehow use it instead of using the object?

I used it: http://www.codingvision.net/miscellaneous/c-load-dll-at-runtime

Ped7g
  • 16,236
  • 3
  • 26
  • 63
18666
  • 125
  • 1
  • 18
  • 1
    You may want to use MEF: https://learn.microsoft.com/en-us/dotnet/framework/mef/ That said, why don't you just cast `instance` to IST? `var instance = Activator.CreateInstance(type) as IST`; – Kevin Gosse Dec 26 '17 at 10:59

1 Answers1

1

use reflection to call methods only there is no other choice.

object instance = Activator.CreateInstance(type);
MethodInfo[] methods = type.GetMethods();
object res = methods[0].Invoke(instance, new object[] { plcConnectionString });

what is methods[0] here, for example?

if IST is a known type in a core lib, and classes in plugin dlls implement IST, do a cast:

IST instance = (IST)Activator.CreateInstance(type);
instance.Start();

now method calls became type-safe

ASh
  • 34,632
  • 9
  • 60
  • 82
  • I change my code: `IST instance = (IST)Activator.CreateInstance(type); var result = instance.WorkRequest(plcConnectionString);` it's work exactly as I wanted. – 18666 Dec 26 '17 at 11:10
  • WorkRequest have parameter on interface I corrected the error – 18666 Dec 26 '17 at 11:12
  • This solution will be efficient? – 18666 Dec 26 '17 at 11:13
  • @18666, it suppose, yes. It is one-time operation. if `appPlugins.ST` doesn't do heavy operations in constructor, of course. I addressed part of the question about access to methods. Please take a look at similar questions: [loading dll in different app domain due to security concerns](https://stackoverflow.com/questions/88717/loading-dlls-into-a-separate-appdomain), [Implementing Plugin Architecture - Dynamic DLL loading](https://stackoverflow.com/questions/6120343/implementing-plugin-architecture-dynamic-dll-loading) – ASh Dec 26 '17 at 11:18