StructureMap Open Generic Types

Posted by Shane Fulmer on Stack Overflow See other posts from Stack Overflow or by Shane Fulmer
Published on 2012-07-10T14:46:25Z Indexed on 2012/07/10 21:15 UTC
Read the original article Hit count: 195

Filed under:
|

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>>?

© Stack Overflow or respective owner

Related posts about c#

Related posts about structuremap