I have following interface
public interface IBuilder<T>
{
T Create(string param);
}
with many classes that implement the interface above. One of them is:
public class ConcreteABuilder : IBuilder<ConcreteA>
{
public ConcreteA Create(string param)
{
return new ConcreteA();
}
}
I’m using StructureMap to register all classes that implement IBuilder<>
Scan(x =>
{
x.TheCallingAssembly();
x.AddAllTypesOf(typeof(IBuilder<>));
});
Now, I have 2 cases
EDITED
I get the types(in both cases) in the form of System.Type
Case 1
At runtime I get any T type(System.Type) (e.g. typeof(ConcreteA)) and I need to get the matched builder instance. In this case it must return ConcreteABuilder instance.
Case 2
At runtime I get the type(System.Type) of some implemented IBuilder(e.g. typeof(ConcreteABuilder)) and I need to get the matched builder instance. In this case it must return ConcreteABuilder instance.
How using StructureMap’s ObjectFactory to solve Case1 & Case2?
Thank you
I think what you’re looking for is registering your types with:
Then asking the container for an
IBuilder<ConcreteA>or aConcreteABuilderwill return aConcreteABuilder… now the problem is that since you don’t know the type until runtime (selected by the user or something?), you can only use the non-generic version:or
and somewhere where you actually know that you’re asking for a an IBuilder that can return ConcreteA