Register and Resolve Generic Interfaces Unity

Posted by user1643791 on Stack Overflow See other posts from Stack Overflow or by user1643791
Published on 2013-11-03T15:03:44Z Indexed on 2013/11/03 15:53 UTC
Read the original article Hit count: 323

Filed under:
|
|

I am trying to register some generic interfaces and resolve them .

I have the registering function

private static void RegisterFolderAssemblies(Type t,string folder)
    {
        var scanner = new FolderGenericInterfaceScanner();
        var scanned = scanner.Scan(t,folder); // gets the implementations from a specific folder
        scanned.ForEach(concrete =>
        {
            if (concrete.BaseType != null || concrete.IsGenericType)
            {
                myContainer.RegisterType(t, Type.GetType(concrete.AssemblyQualifiedName), concrete.AssemblyQualifiedName);
            }
        });
    }

which is called by the bootstrapper with

RegisterFolderAssemblies(typeof(IConfigurationVerification<>),Environment.CurrentDirectory);

The registration seem to go through ok but when I try to Resolve them with

Type generic = typeof(IConfigurationVerification<>);
Type specific = generic.MakeGenericType(input.Arguments[0].GetType());

var verifications = BootStrap.ResolveAll(specific);

The input.Arguments[0] is an object of the type the generic is implemented in I also tried using typeof(IConfigurationVerification<>) instead and get the same error .

When ResolveAll is

public static List<object> ResolveAll(Type t)
{
        return myContainer.ResolveAll(t).ToList();
}

I get a ResolutionFailedException with them message "The current type, Infrastructure.Interfaces.IConfigurationVerification`1[Infrastructure.Configuration.IMLogPlayerConfiguration+LoadDefinitions], is an interface and cannot be constructed. Are you missing a type mapping?"

Any help will be great.

Thanks in advance

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics