2013-11-03 75 views
0

我想注册一些通用接口并解决它们。注册和解析通用接口Unity

我有注册函数

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); 
      } 
     }); 
    } 

这是由引导程序与

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

注册名为似乎要经过好,但是当我尝试解决这些问题

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

var verifications = BootStrap.ResolveAll(specific); 

input.Arguments [0]是通用在中实现的类型的对象我也尝试使用typeof(IConfigurationVerification <>)来代替,并得到相同的错误。

当ResolveAll是

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

我得到一个ResolutionFailedException与他们的消息“当前类型,Infrastructure.Interfaces.IConfigurationVerification`1 [Infrastructure.Configuration.IMLogPlayerConfiguration + LoadDefinitions],是一个接口,并且不能构造你缺少一个类型映射吗?“

任何帮助将是伟大的。

在此先感谢

回答

1

你不能有一个接口的实例,但你从实现该接口类型可以。

interface IFoo{ 
} 

class A : IFoo{ 
} 

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

Unity(或其他DI容器)内的某处使用激活器。

在您可以实例化的类型上过滤您扫描的类型:nonabstract - 类或结构体。如果你不注册那些不能被实例化的类型。

导致您得到的错误。

+0

你是对的,我不知道我是如何错过它的注册接口以及实现。 (我怪一小时) – Amorphis

+0

我知道这次演习,但是......你的投票赞赏:-) – lboshuizen