3

I am probably missing something extremely simple.

I am just trying to write a very minimalistic example of usage of DynamicProxy - I basically want to intercept the call and display method name and parameter value. I have code as follows:

public class FirstKindInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine("First kind interceptor before {0} call with parameter {1} ", invocation.Method.Name, invocation.Arguments[0]);
        invocation.Proceed();
        Console.WriteLine("First kind interceptor after the call");
    }
}

public interface IFancyService
{
    string GetResponse(string request);
}

public class FancyService : IFancyService
{
    public string GetResponse(string request)
    {
        return "Did you just say '" + request + "'?";
    }
}

class Program
{
    static void Main(string[] args)
    {
        var service = new FancyService();
        var interceptor = new FirstKindInterceptor();
        var generator = new ProxyGenerator();
        var proxy = generator.CreateClassProxyWithTarget<IFancyService>(service, new IInterceptor[] { interceptor } );

        Console.WriteLine(proxy.GetResponse("what?"));
    }
}

However, when I run it I get a following exception:

Unhandled Exception: System.ArgumentException: 'classToProxy' must be a class Parameter name: classToProxy

What am I missing?

Sebastian K
  • 6,235
  • 1
  • 43
  • 67
  • 1
    If the error is on `var proxy = generator.CreateClassProxyWithTarget(service, new IInterceptor[] {interceptor});` then one (at least) of the interfaces you are refering to needs to refer to a class. – Dale M Jan 21 '13 at 04:02

1 Answers1

6

The error is that CreateClassProxyWithTarget needs to be a type of class not interface. CreateInterfaceProxyWithTarget uses an interface.

Dale M
  • 2,453
  • 1
  • 13
  • 21