1. 程式人生 > 其它 >如何在Castle.DynamicProxy中使用IInterceptor?(How use IInterceptor in Castle.DynamicProxy?)

如何在Castle.DynamicProxy中使用IInterceptor?(How use IInterceptor in Castle.DynamicProxy?)

參考:https://www.it1352.com/1796724.html

我寫了一個這樣的例子

簡單計算器類:

public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

實現了DynamicProxy提供的"IInterceptor"

 [Serializable]
public abstract class Interceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        ExecuteBefore(invocation);
        invocation.Proceed();
        ExecuteAfter(invocation);

    }
    protected abstract void ExecuteAfter(IInvocation invocation);
    protected abstract void ExecuteBefore(IInvocation invocation);
}

建立了一個Interceptor類,並從"Interceptor"類繼承

    public class CalculatorInterceptor : Interceptor
{
    protected override void ExecuteBefore(Castle.DynamicProxy.IInvocation invocation)
    {
        Console.WriteLine("Start");
    }

    protected override void ExecuteAfter(Castle.DynamicProxy.IInvocation invocation)
    {
        Console.WriteLine("End");
    }
}

但是當我使用它不起作用時!!

static void Main(string[] args)
    {
        ProxyGenerator generator = new ProxyGenerator();
        Calculator c = generator.CreateClassProxy<Calculator>(new CalculatorInterceptor());
        var r = c.Add(11, 22);
        Console.WriteLine(r);
        Console.ReadKey();
    }

我例外地看到了這樣的東西:

START
33
END

但僅顯示

33

我該如何糾正?!

解決方案

嘗試將方法Add設為虛擬.

public class Calculator
{
    public virtual int Add(int a, int b)
    {
        return a + b;
    }
}

代理生成器建立一個繼承Calculator的新類.因此,方法Add獲得重寫以使攔截成為可能.

I wrote an example like this

Simple Calculator class :

public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

implemented "IInterceptor" that provided by DynamicProxy

 [Serializable]
public abstract class Interceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        ExecuteBefore(invocation);
        invocation.Proceed();
        ExecuteAfter(invocation);

    }
    protected abstract void ExecuteAfter(IInvocation invocation);
    protected abstract void ExecuteBefore(IInvocation invocation);
}

Created an Interceptor class and inherited from "Interceptor" class

    public class CalculatorInterceptor : Interceptor
{
    protected override void ExecuteBefore(Castle.DynamicProxy.IInvocation invocation)
    {
        Console.WriteLine("Start");
    }

    protected override void ExecuteAfter(Castle.DynamicProxy.IInvocation invocation)
    {
        Console.WriteLine("End");
    }
}

but when I used it NOT working !!!

static void Main(string[] args)
    {
        ProxyGenerator generator = new ProxyGenerator();
        Calculator c = generator.CreateClassProxy<Calculator>(new CalculatorInterceptor());
        var r = c.Add(11, 22);
        Console.WriteLine(r);
        Console.ReadKey();
    }

I excepted to see something like this :

START
33
END

but only show

33

How I can correct it ?!

解決方案

Try to make the method Add virtual.

public class Calculator
{
    public virtual int Add(int a, int b)
    {
        return a + b;
    }
}

The proxy generator creates a new class inheriting Calculator. Thus, the method Add gets an override to make interception possible.

獲得方法名

System.Reflection.MethodBase.GetCurrentMethod().Name;