Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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# Unity拦截:如何将参数传递到ICallHandler实现中?_C#_Unity Container_Interception_Unity Interception - Fatal编程技术网

C# Unity拦截:如何将参数传递到ICallHandler实现中?

C# Unity拦截:如何将参数传递到ICallHandler实现中?,c#,unity-container,interception,unity-interception,C#,Unity Container,Interception,Unity Interception,我可以像这样将消息参数传递给ICallHandler实现吗: var logic = container.Resolve<IBussinessLogic>(message); IMethodReturn ICallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { Console.WriteLine( string.Format(

我可以像这样将消息参数传递给ICallHandler实现吗:

var logic = container.Resolve<IBussinessLogic>(message);
IMethodReturn ICallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        Console.WriteLine(
            string.Format(
                "Begin {0} with param {1}",
                input.MethodBase.Name, 
                message // parameter I need to be passed
            )
        );

        var result = getNext.Invoke()(input, getNext);

        Console.WriteLine("End " + input.MethodBase.Name);
        return result;
    }

传递给
Resolve
方法的
消息实际上是Unity要构造的命名实例名。Unity使用此值选择要使用的
IBusinessLogic
的哪个实现;在构造实现对象之后,它将丢失


因此,该值仅在对象构造期间处于统一范围内;您的
ICallHandler
无法访问它,因为。

似乎Steve Wilkes是对的:“老实说,这听起来不可能使用拦截。”


我知道此代码不起作用。我写这篇文章是为了描述我的想法。我所需要的就是将参数从resolve调用代码传递到ICallHandler实现。我还发现,以某种方式可以将参数传递给ICallHandler实现构造函数。但ICallHandler构造函数只对每个容器调用一次(正如我测试的那样)。但是每次调用Resolve()时我都需要pass参数。
消息的性质是什么?这是设计时您知道的吗?在我的示例中是字符串。在我的实际应用程序中,它是System.Web.Services.WebService。我的方面试图在调用真正的逻辑之前用WebService实例进行准备。我明白了。老实说,这听起来不可能使用拦截。你可以找一个装饰师来做吗?如果你想更深入地讨论这个问题,请随时给我发电子邮件:)