繁体   English   中英

尝试使用标记接口失败,IEnumerable <>

[英]Attempting to use a marker interface fails with IEnumerable<>

我试图在我自己的应用程序中使用统一来模仿Orchard CMS中的一些东西。

好的,我想要做的就是这......

假设我有一个名为IDependency的标记接口..

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

到目前为止我有什么..

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

我正在向我的NicksSecondController注入一个IEnumerable ...任何想法?

干杯,尼克

开箱即用Unity只知道如何解析数组。 看看codeplex上的TecX项目 它包含一个自定义扩展,它教导容器处理IEnumerable<T>IList<T>ICollection<T> 代码可以在TecX.Unity (文件夹集合 )中找到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM