0
votes

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

1

1 Answers

1
votes

You can't have an instance of an interface, but you can from a type implementing the interface.

interface IFoo{
}

class A : IFoo{
}

Activator.CreateInstance(typeof(IFoo)) //fails;
Activator.CreateInstance(typeof(A)) //succeeds;

Somewhere inside Unity (or an other DI container) Activator is used.

Filter the types you scan on types that you can instantiate: nonabstract-classes or structs. If you don't you also register types that can't be instantiated.

Resulting in the error you've got.