1

Let's say I have got this kind of structure:

|
|-Plugins
         |-Plugin1.cs
         |-Plugin2.cs
         |-Plugin3.cs
|-PluginBase.cs
|-PluginList.cs

where each class in the Plugins folder extends from PluginBase.cs.

I need my PluginList.cs to export a list of the available plugins, so that the app can use it to make the user choose which one he wants.

Now, since this is my first experience with C#, I don't know how this works.

Searching online I found something about the Managed Extensibility Framework but I'm not sure if this is what I need.

My very raw idea is to scan the Plugin directory and list them but I don't know if C# as an equivalent to Java's Class class so I've not tried yet.

Since I'm talking about a mobile application, I also want it to be as fast as possible, so maybe this scanning method is not the best one.

Any idea?

EDIT:

So, the best thing to do is to use MEF2 but I can't make it work because Visual Studio won't let me add the assembly to the project (tried with Nuget and by manually adding the dlls).

Is there any alternative to MEF2?

StepTNT
  • 3,867
  • 7
  • 41
  • 82
  • I think the biggest problem with a plugin system for a WP8 app is that you can't exactly download assemblies and load them at runtime as you might with a pluggable application on the desktop. So, what's the point of plugging if you can't do that? –  Jul 11 '13 at 15:31
  • I may have failed in writing a good question, but I need this to work because I don't want to manually update my code everytime I add a new plugin. Let's say that this in app which can download stuff from various services. Using plugins I don't have to change my code everytime I add a new service but the system will find and enable it automatically. That's all! – StepTNT Jul 11 '13 at 15:47
  • Gotcha. That will work fine, then, as long as you use MEF2. You can use `WithAssembly` to add the plugin assembly to the container and roll from there. But you will definitely not be able to "scan the plugin directory". And there are further restrictions on reflection that may cause you issues on the WP, but I don't know exactly what they will be. You can spin up a quick prototype to test, of course. –  Jul 11 '13 at 16:01

1 Answers1

1

Actually, MEF is exactly what you need. It is designed specifically for plugin or extensible architectures and provides a range of base functionality that you are trying to implement yourself.

The Managed Extensibility Framework (MEF) is a composition layer for .NET that improves the flexibility, maintainability and testability of large applications. MEF can be used for third-party plugin extensibility, or it can bring the benefits of a loosely-coupled plugin-like architecture to regular applications.

http://mef.codeplex.com/

This link provides a nice introduction and basic example to explain how MEF works.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
dsfgsho
  • 2,731
  • 2
  • 22
  • 39
  • Followed setup instructions here http://nuget.org/packages/microsoft.composition but it's not working. As I said, I'm targeting WP8 and the package seems not available on WP. – StepTNT Jul 11 '13 at 14:31
  • http://stackoverflow.com/questions/12483686/how-to-use-mef-in-portable-class-library – dsfgsho Jul 11 '13 at 14:39
  • No way. I'm manually adding dll references using the package downloaded from CodePlex but it says it cannot add the assembly to the project (I don't know what's the error message in English but it talks about compatibility) – StepTNT Jul 11 '13 at 14:47
  • MEF isn't supported for PCLs. You can check out the list here: http://msdn.microsoft.com/en-us/library/gg597391.aspx Also MEF is now part of the framework, so that codeplex link is out of date. –  Jul 11 '13 at 15:29
  • Hmmm, MEF2 apparently supports PCLs, so that might be an option http://nuget.org/packages/microsoft.composition (but I really don't think the OP will find much use in it) –  Jul 11 '13 at 15:33
  • I've already tried with the latest link but it won't install on a WP8 project. @StevenHouben suggested a workaround but it still doesn't work as I can't even manually add the dll to the project. – StepTNT Jul 11 '13 at 15:42
  • @StepTNT: I missed the fact that they are still using that codeplex site for MEF2, so I was wrong. You might try creating a PCL library and then add the NuGet package for MEF2 to that. If that doesn't work, then you're SOL. But not really, there are plenty of IoC containers on NuGet that are WP8 compat. That's really all you need. –  Jul 11 '13 at 16:07
  • @StepTNT: well, without proper error message there is not much I can say. The answer in the link is by one of the maintainers and clearly shows how to do the work around. How is your project setup different? – dsfgsho Jul 11 '13 at 17:00
  • I don't think that writing the error message in Italian will help :D It says that the assembly is not compatible with my project. My app has been set up using the "New Windows Phone Application" template and I'm targeting WP8 – StepTNT Jul 11 '13 at 17:09