Get instance of type inheriting from base class, implementing interface, using StructureMap

Posted by Ben on Stack Overflow See other posts from Stack Overflow or by Ben
Published on 2010-04-27T10:05:16Z Indexed on 2010/04/27 12:53 UTC
Read the original article Hit count: 332

Filed under:
|
|

Continuing on my quest for a good plugin implementation I have been testing the StructureMap assembly scanning features.

All plugins will inherit from abstract class PluginBase. This will provide access to common application services such as logging. Depending on it's function, each plugin may then implement additional interfaces, for example, IStartUpTask.

I am initializing my plugins like so:

            Scan(x => {
            x.AssembliesFromPath(HttpContext.Current.Server.MapPath("~/Plugins"),
                assembly => assembly.GetName().Name.Contains("Extension"));             
            x.AddAllTypesOf<PluginBase>();
        });

The difficulty I am then having is how to work against the interface (not the PluginBase) in code. It's easy enough to work with PluginBase:

            var plugins = ObjectFactory.GetAllInstances<PluginBase>();

        foreach (var plugin in plugins)
        {

        }

But specific functionality (e.g. IStartUpTask.RunTask) is tied to the interface, not the base class.

I appreciate this may not be specific to structuremap (perhaps more a question of reflection).

Thanks, Ben

© Stack Overflow or respective owner

Related posts about structuremap

Related posts about reflection