Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>your message handlers can be seen as plugins which makes your problem a potential fit for the <a href="http://mef.codeplex.com/wikipage?title=Overview&amp;referringTitle=Home" rel="nofollow noreferrer">Managed Extensibility Framework</a>. Since .Net 4 it's also shipped with the .Net framework.</p> <p>You can find sample introductions to MEF <a href="http://blogs.msdn.com/b/brada/archive/2009/07/20/simple-example-using-managed-extensibility-framework-in-silverlight.aspx" rel="nofollow noreferrer">here</a> and <a href="http://randomactsofcoding.blogspot.com/2009/11/introduction-to-mef.html" rel="nofollow noreferrer">here</a>.</p> <p>I've put together a litte example to show that it's quite simple to use basic MEF functionality (although there is much more you can do with it). First there is a <code>PluginHost</code> class which will host the plugins in its <code>Plugins</code> collection. Then there's a simple interface containing just the property <code>Description</code> and an example implementation of a plugin called <code>ExamplePlugin</code>.</p> <p>The <code>Plugins</code> collection will be filled by the <code>container.ComposeParts(..)</code> method called in the constructor. All that's required to make that magic happen are the <code>[Export]</code> and <code>[ImportMany]</code> attributes.</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; namespace Playground { public class Program { static void Main(string[] args) { PluginHost host = new PluginHost(); host.PrintListOfPlugins(); Console.ReadKey(); } } public class PluginHost { [ImportMany] public IEnumerable&lt;IPlugin&gt; Plugins { get; set; } public PluginHost() { var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); container.ComposeParts(this); } public void PrintListOfPlugins() { foreach (IPlugin plugin in Plugins) Console.WriteLine(plugin.Description); } } public interface IPlugin { string Description { get; } } [Export(typeof(IPlugin))] public class ExamplePlugin : IPlugin { #region IPlugin Members public string Description { get { return "I'm an example plugin!"; } } #endregion } } </code></pre> <p>UPDATE: You can use so called <a href="http://mef.codeplex.com/wikipage?title=Using%20Catalogs&amp;referringTitle=Guide" rel="nofollow noreferrer">Catalogs</a> to discover plugins in more than one assembly. For example there is a <code>DirectoryCatalog</code> which gives you all exports found in all assemblies in a given directory.</p> <p><code>AppDomain.CurrentDomain.GetAssemblies();</code> returns an array of all assemblies loaded into the current AppDomain. You could then iterate over that array to create an AggregateCatalog containing an AssemblyCatalog per loaded assembly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload