Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 城堡温莎类型工厂关闭打开通用_C#_Generics_Dependency Injection_Castle Windsor - Fatal编程技术网

C# 城堡温莎类型工厂关闭打开通用

C# 城堡温莎类型工厂关闭打开通用,c#,generics,dependency-injection,castle-windsor,C#,Generics,Dependency Injection,Castle Windsor,我不确定这是否可行,但我们希望做到以下几点 我们有处理一类消息的处理器。每条消息都有一个MessageOrigin。处理器需要基于MessageOrigin的不同映射器。处理器对MessageOrigin不感兴趣 这为我们提供了以下代码(完整的要点) 与选择器结合使用: public class ProcessorSelector : DefaultTypedFactoryComponentSelector { protected override Type GetComponentTy

我不确定这是否可行,但我们希望做到以下几点

我们有处理一类消息的处理器。每条消息都有一个MessageOrigin。处理器需要基于MessageOrigin的不同映射器。处理器对MessageOrigin不感兴趣

这为我们提供了以下代码(完整的要点)

与选择器结合使用:

public class ProcessorSelector : DefaultTypedFactoryComponentSelector
{
    protected override Type GetComponentType(MethodInfo method, 
        object[] arguments)
    {
        return typeof(IProcessor<,>).MakeGenericType(arguments[0].GetType(),
            arguments[1].GetType()).MakeArrayType();
    }
}
公共类处理器选择器:DefaultTypedFactoryComponentSelector
{
受保护的重写类型GetComponentType(MethodInfo方法,
对象[]参数)
{
返回typeof(IPProcessor).MakeGenericType(参数[0]。GetType(),
参数[1].GetType()).MakeArrayType();
}
}

然而,当我们调用
ipProcessorFactory
时,我们从未得到处理器。我猜这是因为在容器中注册类型时,tmessagorigin仍然处于打开状态。如何解决这个问题?

根据我在您的要点中看到的情况,您没有一个指定两种泛型类型的
ProcessorImp
实现。这当然是工厂无法解析
ipprocessor
的原因:您试图解析服务
ipprocessor
,但您注册的组件实现了

IProcessor<Message, TMessageOrigin>
where TMessageOrigin : IMessageOrigin
i处理器
TMessagorigin在哪里:IMessagorigin

也许您应该重新思考您尝试注册/解析服务的方式;您的
MessageOriginImpl
可能有一个接口
iisorigin
,工厂会根据
MessageImpl

的类型解决这些问题,我知道没有注册了确切通用封装外形的处理器,但是我想知道Castle是否可以使用选择器中的参数为我关闭最后一个打开的泛型值。我认为Castle无法关闭这个挂起的泛型值,您必须在工厂中显式解决它。这就是为什么我建议重新考虑注册/解析服务的方式。如果一条消息只有一个MessageOrigin,那么不需要IPProcessor知道MessageOrigin,因为它可以从消息中解析。如果每条消息有多个消息来源,那么您必须选择系统必须如何解析和处理它们。每条消息有多个来源。我们重新考虑了系统,但我认为可能有一种方法可以关闭Castle中的泛型,某种程度上涉及到
IGenericImplementationMatchingStrategy
@Lodewijk我确实查看了
IGenericImplementationMatchingStrategy
,但我找不到第二种类型自动折叠的情况。甚至这里的示例也使用了从实现到自动解决的直接匹配:
public class ProcessorSelector : DefaultTypedFactoryComponentSelector
{
    protected override Type GetComponentType(MethodInfo method, 
        object[] arguments)
    {
        return typeof(IProcessor<,>).MakeGenericType(arguments[0].GetType(),
            arguments[1].GetType()).MakeArrayType();
    }
}
IProcessor<Message, TMessageOrigin>
where TMessageOrigin : IMessageOrigin