Created
November 20, 2012 16:34
-
-
Save mdissel/4119071 to your computer and use it in GitHub Desktop.
Register PluginFamily with structuremap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void PluginTest() { | |
using (IContainer container = new Container()) { | |
container.Configure(x => { | |
x.Scan(y => { | |
y.TheCallingAssembly(); | |
y.Convention<PluginFamilyScanner>(); | |
}); | |
}); | |
container.AssertConfigurationIsValid(); | |
IFoo foo2 = container.GetInstance<IFoo>(); | |
Assert.That(foo2.GetType().Equals(typeof(Foo2))); | |
IEnumerable<IFoo> foos = container.GetAllInstances<IFoo>(); | |
Assert.That(foos.Count(), Is.EqualTo(2)); | |
} | |
} | |
[PluginFamily()] | |
public interface IFoo {} | |
public class Foo : IFoo {} | |
public class Foo2 : IFoo {} | |
public class PluginFamilyScanner : IRegistrationConvention { | |
public void Process(Type type, Registry registry) { | |
if (!type.IsConcrete()) return; | |
foreach (Type iType in type.GetInterfaces()) { | |
if (iType == null || !PluginFamilyAttribute.MarkedAsPluginFamily(iType)) | |
return; | |
if (PluginCache.GetPlugin(iType) != null) { | |
if (type.CanBeCastTo(iType) && Constructor.HasConstructors(type)) { | |
registry.For(iType).Use(type); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment