小编hib*_*tor的帖子

Unity:将两个接口注册为一个带有拦截的单例

我有一个实现两个接口的类,我想对类的方法应用拦截.

我遵循Unity中的建议将两个接口作为一个单独的接口,但我对结果感到惊讶.简而言之,似乎我的CallHandler被调用了两次.我有一个最简单的例子是:

public interface I1
{
    void Method1();
}

public interface I2
{
    void Method2();
}

public class C : I1, I2
{
    [Log]
    public void Method1() {}

    public void Method2() {}
}

public class LogAttribute : HandlerAttribute
{
    public override ICallHandler CreateHandler(IUnityContainer container)
    {
        return new LogCallHandler();
    }
}

public class LogCallHandler : ICallHandler
{
    public IMethodReturn Invoke(
        IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        Console.WriteLine("Entering " + input.MethodBase.Name);
        var methodReturn = getNext().Invoke(input, getNext);
        Console.WriteLine("Leaving " + …
Run Code Online (Sandbox Code Playgroud)

c# unity-container unity-interception

6
推荐指数
1
解决办法
3259
查看次数

标签 统计

c# ×1

unity-container ×1

unity-interception ×1