StructureMap Open Generic Types
- by Shane Fulmer
I have the following classes:
public interface IRenderer<T>
{
string Render(T obj);
}
public class Generic<T> { }
public class SampleGenericRenderer<T> : IRenderer<Generic<T>>
{
public string Render(Generic<T> obj)
{
throw new NotImplementedException();
}
}
I would like to be able to call StructureMap with
ObjectFactory.GetInstance<IRenderer<Generic<string>>>(); and receive SampleGenericRenderer<string>.
I'm currently using the following registration and receiving this error when I call GetInstance. "Unable to cast object of type 'ConsoleApplication1.SampleGenericRenderer'1[ConsoleApplication1.Generic'1[System.String]]' to type 'ConsoleApplication1.IRenderer'1[ConsoleApplication1.Generic'1[System.String]]'."
public class CoreRegistry : Registry
{
public CoreRegistry()
{
Scan(assemblyScanner =>
{
assemblyScanner.AssemblyContainingType(typeof(IRenderer<>));
assemblyScanner.AddAllTypesOf(typeof(IRenderer<>));
assemblyScanner.ConnectImplementationsToTypesClosing(typeof(IRenderer<>));
});
}
}
Is there any way to configure StructureMap so that it creates SampleGenericRenderer<string> instead of SampleGenericRenderer<Generic<string>>?