【发布时间】:2013-12-13 00:19:59
【问题描述】:
我正在尝试为没有目标的接口创建代理,当我在生成的代理上调用方法时,我总是得到 System.NullReferenceException,尽管 拦截器 总是被很好地调用。
这里是接口和拦截器的定义:
internal class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine(invocation.Method.Name);
}
}
public interface IMyInterface
{
int Calc(int x, int y);
int Calc(int x, int y, int z);
}
这里是主程序:
class Program
{
static void Main(string[] args)
{
ProxyGenerator generator = new ProxyGenerator();
IMyInterface proxy = (IMyInterface) generator.CreateInterfaceProxyWithoutTarget(
typeof(IMyInterface), new MyInterceptor());
proxy.Calc(5, 7);
}
}
拦截器被调用,但我从 DynamicProxyGenAssembly2 得到一个异常。为什么?
【问题讨论】: