Castle DynamicProxy - 'classToProxy'必须是一个类

Seb*_*n K 3 .net c# castle-dynamicproxy

我可能遗漏了一些非常简单的东西.

我只是想写一个非常简约的DynamicProxy使用示例 - 我基本上想拦截调用并显示方法名称和参数值.我的代码如下:

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?"));
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行它时,我得到以下异常:

未处理的异常:System.ArgumentException:'classToProxy'必须是类参数名:classToProxy

我错过了什么?

Dal*_*e M 5

错误是CreateClassProxyWithTarget需要是一种类而不是接口.CreateInterfaceProxyWithTarget使用界面.