是否可以将包含所有接口的程序集的名称传递给Castle,并让Castle自动查找并注册当前目录中包含这些接口实现的所有程序集?
下面的代码为我解决了这个问题。如果你看到任何问题,请发表评论。
var InterfacesAssemblyName = /* get assembly name from appSettings */;
if (!string.IsNullOrEmpty(InterfacesAssemblyName))
{
var interfaces = Assembly.Load(InterfacesAssemblyName).GetTypes().Where(type => type.IsInterface && type.IsPublic).ToList();
this._container.Register(
Classes
.FromAssemblyInDirectory(new AssemblyFilter("bin"))
.Where(x => x.GetInterfaces().Intersect(interfaces).Any())
.LifestyleTransient()
.WithService.DefaultInterfaces());
}