0

I have the following class:

public class MyTest
{
    public void Test()
    {

    }
}

and I created the following interceptor:

public class MyInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        invocation.Proceed();
    }
}

and in my code, I do:

        ProxyGenerator g = new ProxyGenerator();

        g.CreateClassProxy<MyTest>(new MyInterceptor());

        MyTest t = new MyTest();
        t.Test();

Shouldn't that hit the Intercept method in the debugger? It is not. Am I missing something?

EDIT: this is specific to Castle DynamicProxy.

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • 1
    Duplicate https://stackoverflow.com/questions/28518700/how-use-iinterceptor-in-castle-dynamicproxy. You should mark `Test` method as `virtual`. – andrew Oct 03 '17 at 00:17
  • Also this `MyTest t = new MyTest();` is not using any proxy, but just create a new object. `var t = g.CreateClassProxy(new MyInterceptor());` - this create a proxy object, so next you can call to its method: `t.Test();` – andrew Oct 03 '17 at 00:39
  • 2
    Possible duplicate of [How use IInterceptor in Castle.DynamicProxy?](https://stackoverflow.com/questions/28518700/how-use-iinterceptor-in-castle-dynamicproxy) – andrew Oct 03 '17 at 00:40

2 Answers2

2

You have to make public void Test() to be public virtual void Test() so Castle DynamicProxy is allowed to intercept that method.

A dynamic proxy is a way of generating a subclass from a class or interface of which is generally a model. That subclass overrides every method that it can (make your methods virtual to allow to do it).

For more documents about Castle Dynamic Proxy:

https://richardwilburn.wordpress.com/2009/12/17/using-castles-dynamic-proxy/

http://putridparrot.com/blog/dynamic-proxies-with-castle-dynamicproxy/

Jeric Cruz
  • 1,899
  • 1
  • 14
  • 29
0

CreateClassProxy creates so called Inheritance-based proxy. Inheritance-based proxies are created by inheriting a base class. The proxy intercepts calls to virtual members of the class and forwards them to base implementation. Thus only virtual members of the class can be intercepted. In your sample you should mark Test method as virtual. See here.

public class MyTest
{
    public virtual void Test()
    {
        Console.WriteLine("Hi");
    }
}

public class MyInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine("Was here");
        invocation.Proceed();
    }
}

void Main()
{
    ProxyGenerator g = new ProxyGenerator();
    var t = g.CreateClassProxy<MyTest>(new MyInterceptor());
    t.Test();
}
andrew
  • 412
  • 2
  • 14
  • Hmm... so if I don't have control over the base class (it's a .Net class), there is no way to hook the method? – SledgeHammer Oct 03 '17 at 04:33
  • @SledgeHammer There is some tricks http://kozmic.net/2009/02/23/castle-dynamic-proxy-tutorial-part-vi-handling-non-virtual-methods/, but I don't tried them. – andrew Oct 03 '17 at 05:09
  • @SledgeHammer Another option described in https://stackoverflow.com/questions/11763166/how-to-intercept-a-call-to-a-nonvirtual-method-from-to-thirdy-party-libraries-in – andrew Oct 03 '17 at 05:13