3
我可能错过了一些非常简单的事情。Castle DynamicProxy - 'classToProxy'必须是类
我只是想写一个非常简单的动态代理使用示例 - 我基本上想拦截调用并显示方法名称和参数值。我的代码如下:
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
我错过了什么?
如果错误出现在'var proxy = generator.CreateClassProxyWithTarget(service,new IInterceptor [] {interceptor});'那么您引用的一个(至少)接口需要引用一个类。 –