C# 尝试使用标记接口失败,IEnumerable<&燃气轮机;
我试图在我自己的应用程序中使用unity模仿Orchard CMS中的一些内容 好的,所以我想做的是 假设我有一个名为IDependency的标记接口C# 尝试使用标记接口失败,IEnumerable<&燃气轮机;,c#,inversion-of-control,unity-container,ioc-container,auto-registration,C#,Inversion Of Control,Unity Container,Ioc Container,Auto Registration,我试图在我自己的应用程序中使用unity模仿Orchard CMS中的一些内容 好的,所以我想做的是 假设我有一个名为IDependency的标记接口 public interface IDependency{ } 然后我有几个接口挂在这个 public interface ICustomerService : IDependency { } public interface ICustomerRepository : IDependency { } 然后也有一些课程 public cla
public interface IDependency{ }
然后我有几个接口挂在这个
public interface ICustomerService : IDependency { }
public interface ICustomerRepository : IDependency { }
然后也有一些课程
public class CustomerService : ICustomerService {
public CustomerService(ICustomerRepository customerRepo){ }
}
public class SomOtherCustomerService : ICustomerService {
public CustomerService(ICustomerRepository customerRepo){ }
}
public class NicksController : Controller {
public NicksController(IEnumerable<ICustomerService> customerServices) { }
}
public class NicksSecondController : Controller {
public NicksSecondController(IEnumerable<ICustomerService> customerServices) { }
}
公共类CustomerService:ICCustomerService{
公共CustomerService(ICCustomerRepository customerRepo){}
}
公共类SomOtherCustomerService:ICCustomerService{
公共CustomerService(ICCustomerRepository customerRepo){}
}
公共类NicksController:控制器{
公共NicksController(IEnumerable customerServices){}
}
公共类NicksSecondController:控制器{
公共NickSecondController(IEnumerable customerServices){}
}
到目前为止我所拥有的
var container = new UnityContainer();
container
.ConfigureAutoRegistration()
.ExcludeSystemAssemblies()
//.Include(If.Implements<IDependency>, Then.Register()
//.As<IDependency>().UsingLifetime<PerResolveLifetimeManager>())
.Include(t =>
typeof(IDependency).IsAssignableFrom(t), (type, builder) =>
{
builder.RegisterType(type);
foreach (var interfaceType in type.GetInterfaces()
.Where(itf => typeof(IDependency).IsAssignableFrom(itf))) {
builder = builder.RegisterType(interfaceType, type);
}
})
.ApplyAutoRegistration();
var container=newunitycontainer();
容器
.配置自动注册()
.ExcludeSystemAssembly()除外
//.Include(如果是.Implements,那么是.Register())
//.As().UsingLifetime())
.包括(t=>
typeof(IDependency).IsAssignableFrom(t),(type,builder)=>
{
builder.RegisterType(类型);
foreach(type.GetInterfaces()中的var interfaceType)
.Where(itf=>typeof(IDependency).IsAssignableFrom(itf))){
builder=builder.RegisterType(interfaceType,type);
}
})
.ApplyAutoRegistration();
我在我的NicksSecond控制器上注射了一个IEnumerable后摔倒了。。。有什么想法吗
干杯,尼克开箱即用的Unity只知道如何解析数组。看一看这张照片。它包含一个自定义扩展,该扩展教导容器处理
IEnumerable
、IList
和ICollection
。代码可以在TecX.Unity(文件夹集合)中找到