1

I have a "frontend" form application.

In my application the user can chooses external dll assemblies from different directoryes:

plugins_repository_directory  
|
|    first_item_directory   
|        Plugin.dll
|        ...
|
|    second_item_directory   
|        Plugin.dll
|        ...
|     
|    last_item_directory
|        Plugin.dll

All the external assemblies have the same namespace and some common features:

Every "Plugin" have a UserControl.EntryPointView class and a static PluginInfos class.

Ok

When the user choose a plugin from file-system i do the following:

public void LoadPlugin(string fileName){

    myPlugin= Assembly.LoadFile(fileName) //remember to use LoadFile instead of Load

    Type entryPointViewType= myPlugin.GetType("Plugin.EntryPointView");

    UserControl myPluginView (UserControl)Activator.CreateInstance(entryPointViewType);

    myFrontendPanel.Controls.add(myPluginView);

    ((IEntryPointView)myPluginView).initialize(Stuff somestuff);

    //and so on...
}

Now the user can work on his plugin loaded inside the front-end panel.. very good

After a while the user want to load a different plugin, so he chooses a different dll file and basically I dispose the old EntryPointView instance and I run LoadPlugin() again.

I think you are understanding what I am trying to achieve: Various plugins assemblies have the same name/namespace because I want to quick build new plugins via copy-past plugin project -> add/remove/change functionalities -> build as dll -> et voilà

Now it seems legit to me but i am not very experienced architect so I want to ask some question:

  • Am I doing it right?
  • Is there any better and simple way to achieve my goals?
  • Is there some sneaky trap laying under my architecture?
  • Is there a way to "unload" from memory old plugin assembly when the user chose another one?

thank you in advance

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Tara Rulez
  • 13
  • 1
  • 4
  • Something to be aware of when loading in dll files is that it becomes harder to debug. This may not seem like a big deal when your project is small but when it scales it can be very frustrating. – Chronicle Jul 18 '18 at 11:00
  • Could you use a project template rather than a copy/paste project? That way it'll use the ProjectName/Namespace that you specify at creation, just like creating "normal" projects. https://learn.microsoft.com/en-us/visualstudio/ide/how-to-create-project-templates –  Jul 18 '18 at 11:12
  • Unloading assemblies is a whole separate question. If you're using .net core then I don't know if that's possible. If you're using .Net Framework then it's possible but painful. You'll need to set up AppDomains that you load the assemblies in to, then you can unload the entire AppDomain. But you have to be careful about how you communicate between the AppDomains. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/assemblies-gac/how-to-load-and-unload-assemblies https://blogs.msdn.microsoft.com/suzcook/2003/07/08/unloading-an-assembly/ –  Jul 18 '18 at 11:18

0 Answers0