C# 将参数传递给Castle Windsor类型的工厂

C# 将参数传递给Castle Windsor类型的工厂,c#,.net,dependency-injection,castle-windsor,C#,.net,Dependency Injection,Castle Windsor,我下面介绍如何使用类型化工厂并将参数传递给构造函数。 当我试图传递代码中所示的2个参数(1,“fo”)时,类型化工厂给了我这个错误 public class SomeClass { public ITypedFactory2 F2 { get; set; } public void SomeFunction() { var req = F2.Create<IGetFooRequest>(1, "fo"); // ERROR HERE } }

我下面介绍如何使用类型化工厂并将参数传递给构造函数。 当我试图传递代码中所示的2个参数(1,“fo”)时,类型化工厂给了我这个错误

public class SomeClass {
    public ITypedFactory2 F2 { get; set; } 
    public void SomeFunction() {
        var req = F2.Create<IGetFooRequest>(1, "fo"); // ERROR HERE
    }
}
public class GetFooRequest : IGetFooRequest {
    public int Bar { get; private set; }
    public string Ton { get; private set; }
    public GetFooRequest(int bar, string ton ) {
        Bar = bar;
        Ton = ton;
    }
}
public interface IGetFooRequest{
    int Bar { get; }
    string Ton { get; }
}    
public interface ITypedFactory2 {
    T Create<T>(int param1, string param2);
    void Release(object t);
}

公共类SomeClass{
公共ITypedFactory2 F2{get;set;}
公共函数(){
var req=F2.Create(1,“fo”);//此处出错
}
}
公共类GetFooRequest:IGetFooRequest{
公共整型条{get;private set;}
公共字符串Ton{get;private set;}
公共GetFooRequest(整型条,字符串吨){
巴=巴;
吨=吨;
}
}
公共接口IGetFooRequest{
整型条{get;}
字符串{get;}
}    
公共接口ITypedFactory2{
T创建(int-param1,string-param2);
无效释放(对象t);
}
这是温莎安装程序的一部分

container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<ITypedFactory2>().AsFactory());
container.Register(AllTypes
            .FromAssemblyContaining<IGetFooRequest>()
            .Where(type => type.Name.EndsWith("Request"))
            .WithService.AllInterfaces().LifestyleTransient());
container.AddFacility();
container.Register(Component.For().AsFactory());
容器.寄存器(所有类型)
.FromAssemblyContaining()
.Where(type=>type.Name.EndsWith(“请求”))
.WithService.AllInterfaces().LifestyleTransient());

为什么它说无法解决非可选依赖项。。。?我已通过(1,“fo”);我真的不明白为什么会这样。。。请帮忙。

我看了看自己的代码,说,(int-param1,string-param2)看起来不太好。让我使用(int-bar,string-ton)。。。命名解决了这个问题。哈哈,难以置信,而且我看不到文件提到命名的重要性


幸运的是,我确实记得,这些依赖项首先是按名称解析的,然后是按类型解析的。这就是名字部分在工作,而类型部分在水中。无论如何,我很高兴我知道了如何使用它,所以我在这里与任何需要它的人分享我的答案。

我有同样的问题,刚刚找到了答案。工厂方法和类构造函数的参数名称必须匹配,不区分大小写

因此,将您的工厂界面更改为

public interface ITypedFactory2 {
    T Create<T>(int **bar**, string **ton**);
    void Release(object t);
}

如果要按类型而不是参数名称进行解析,可以创建自己的
组件选择器

公共类ComponentSelector:DefaultTypedFactoryComponentSelector
{
受保护的重写参数GetArguments(MethodInfo方法,对象[]参数)
{
if(参数==null)
返回null;
Arguments argumentMap=新参数();
ParameterInfo[]parameters=method.GetParameters();
列表类型=参数;
列出duplicateTypes=types.Where(t=>types.Count(type=>type==t)>1.ToList();
对于(int i=0;i
正如您在我的实现中看到的,您需要处理构造函数具有多个相同类型参数的情况。
在这种情况下,您必须按参数名称进行解析,因为
Castle.Windsor
将对具有相同类型的每个参数使用类型的第一个参数

要使用您自己的
组件选择器
,您还必须在
IWindsorContainer
中注册它:

container.Register(Component.For());
最后,您必须告诉工厂使用自己的
组件选择器
作为其组件选择器:

container.Register(Component.For().AsFactory(f=>f.SelectedWith());

有关更多信息,请查看如何使用自定义组件选择器。

此响应基本上重复了前面的回答。Wabble提供的答复应标记为此处的答案。@不要看日期,因为我在Wabble调查此事前一年回答了自己的问题。安威,我在他的帖子上标出答案。
public class GetFooRequest : IGetFooRequest {
    public int Bar { get; private set; }
    public string Ton { get; private set; }
    public GetFooRequest(int **param1**, string **param2**) {
        Bar = bar;
        Ton = ton;
    }
}