3
A
回答
6
是的,这是可能的。默认组件激活器仅用于公共构造函数。 您可以提供该组件,将采取内部构造考虑,或使用例如工厂,以激活组件定制组件激活:
var container = new WindsorContainer()
.AddFacility<FactorySupportFacility>()
.Register(Component.For<Foo>()
.UsingFactoryMethod(() => new Foo()));
var foo = container.Resolve<Foo>();
但是你真的应该重新考虑使.ctor放在首位内部。这样做确实不是一个好主意,特别是当你将课程作为外部世界的组件暴露出来时。
0
按照接受的答案的建议下,我才得以延长DefaultComponentActivator
类protected
建设者的工作(它仍然无法与private
或internal
工作;下面的代码工作正常,那就是,但在别的东西DynamicProxy创建链失败)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Castle.Core;
using Castle.MicroKernel;
using Castle.MicroKernel.ComponentActivator;
using Castle.MicroKernel.Context;
namespace /* YourNameSpaceHere */
{
[Serializable]
public class NonPublicComponentActivator : DefaultComponentActivator
{
public NonPublicComponentActivator(ComponentModel model, IKernelInternal kernel, ComponentInstanceDelegate onCreation, ComponentInstanceDelegate onDestruction)
: base(model, kernel, onCreation, onDestruction)
{ /* do nothing */ }
private readonly List<Type> loadedTypes = new List<Type>();
protected override ConstructorCandidate SelectEligibleConstructor(CreationContext context)
{
lock (loadedTypes)
{
if (!loadedTypes.Contains(context.RequestedType))
{
loadedTypes.Add(context.RequestedType);
// Add the missing non-public constructors too:
var ctors = context.RequestedType.GetConstructors
(
BindingFlags.NonPublic | BindingFlags.Instance
);
foreach (var ctor in ctors)
{
Model.AddConstructor
(
new ConstructorCandidate
(
ctor,
ctor.GetParameters().Select(pi => new ConstructorDependencyModel(pi)).ToArray()
)
);
}
}
}
return base.SelectEligibleConstructor(context);
}
}
}
然后,您的容器上,你通过通用Activator
方法针对ComponentRegistration
对象登记本,所以我container.Register调用看起来是这样的:
_container.Register
(
// . . .
AllClasses.FromThisAssembly().BasedOn<ISomeInterface>()
.Configure
(
c => c.LifeStyle.Transient
.Interceptors<MyInterceptor>()
.Activator<NonPublicComponentActivator>() // <--- REGISTERED HERE
),
// . . .
);
希望帮助别人!
相关问题
- 1. 温莎城堡注册实施通过构造函数
- 2. 温莎城堡内部构造函数/类
- 3. 温莎城堡&注册ApplicationContext
- 4. 城堡温莎构造器注入
- 5. 注册AutoMapper 5.1.1与温莎城堡
- 6. 与温莎城堡
- 7. 温莎城堡 - 无法注册argumants
- 8. 城堡温莎自动注册
- 9. 温莎城堡注册装饰师
- 10. 城堡温莎注册开仿制药
- 11. 城堡温莎多个服务注册
- 12. 城堡温莎注册 - AddAdditionalInterfaces()实施UsingFactoryMethod()
- 13. 城堡温莎复杂通用注册
- 14. 温莎城堡:多构造问题
- 15. 与构造温莎城堡寄存器类参数
- 16. 温莎城堡:如何注册内部实现
- 17. 城堡温莎 - 不刺默认构造函数参数
- 18. 使用与温莎城堡
- 19. 温莎城堡与UsingFactoryMethod LifestyleTransient
- 20. 与温莎城堡在C#
- 21. Umbraco MVC与城堡温莎
- 22. 在温莎城堡
- 23. 城堡温莎Castle.MicroKernel.ComponentRegistrationException
- 24. ComponentNotFoundException城堡温莎
- 25. 在温莎城堡
- 26. 与构造温莎城堡自动登记参数
- 27. 温莎城堡:按照公约注册并命名部分
- 28. 城堡温莎注册的开放式通用装饰类型
- 29. 在城堡注册泛型类型温莎
- 30. 如何获得与温莎城堡的自动注册
嗨Krzystzof,我认为“如果它是如何完成的?”这个问题是隐含的!我猜不是。所以: 你怎么做到的? 干杯 Joni – joniba 2010-03-03 13:20:15
谢谢伊恩。所以实际上我必须创建一个工厂来使用反射来构建这些组件。因为Castle Windsor已经在使用反射来进行组件注册,所以感到羞耻。但我理解这个推理。 再次感谢。 – joniba 2010-03-03 14:11:51
+1表示重新考虑设计 – 2010-03-03 15:18:45