我可能错过了一些非常简单的东西。
我只是想写一个非常简约的 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?"));
}
}
但是,当我运行它时,出现以下异常:
未处理的异常:System.ArgumentException:“classToProxy”必须是类参数名称:classToProxy
我错过了什么?