jeudi 19 octobre 2017

How to load a class, with function, from DLL, at run-time, and detect that function's argument type?

How can I export a class, with a single member method, from a C# DLL in such a way that an application that loads that DLL can determine the parameter type of that member method? I'm currently using MEF to assist with export and import discovery of types.

I've created an interface so that I can specify that the class, in the DLL, has a method called Run. So just something like ...

public interface IPlugin
{
  void Run(object args);
}

My current predicament is that I want that Run function to specify its actual parameter type. So, each DLL ("plugin" in my scenario, because I guess I'm going after some basic plugin architecture) would export a class type that subscribes to IPlugin, but would be able to also specify the parameter type. I need to pass in a bit of data into each plugin, and I want to use data transport class types to do so. So, in reality, I'd like to have some various DLLs that would export classes like this ...

public class CatPlugin : IPlugin
{
  void Run(CatPluginPayload payload)
  {
    Console.WriteLine(payload.Age);
  }
}

public class DogPlugin : IPlugin
{
  void Run(DogPluginPayload payload)
  {
    Console.WriteLine(payload.Name);
  }
}

And so on ... with the focus being on the parameter type changing.

I need to be able to load these classes, from my application, at run-time, and be able to call the Run function and pass in the appropriate type.

Like I mentioned earlier, I am using MEF to assist with the exporting and importing, but I don't see that it can assist me with this, because this seems to require a generic interface or something.

How can I achieve what I'm aiming for?





Aucun commentaire:

Enregistrer un commentaire