我正在使用大量未记录的Castle动态代理系统.我设法让它做我想要的几乎所有事情,除了一件事:你如何使代理方法抛出异常而不是返回值?
public sealed class MyInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { if (CheckArgs(invocation.Arguments)) { invocation.ReturnValue = DoRealWork(invocation.Arguments); } else { invocation.Exception = new InvalidOperationException(); // How? } } }
解决方法
从代理对象的角度来看,拦截器是不可见的;它只是调用自己的虚方法,DynamicProxy在将ReturnValue返回给调用者之前调用正确的拦截器方法.
因此,如果你想抛出异常只是从拦截器抛出它:
if (CheckArgs(invocation.Arguments)) { invocation.ReturnValue = DoRealWork(invocation.Arguments); } else { throw new InvalidOperationException(); }
编辑评论:
关于生成器中抛出的异常的类型,我有正确的类型,而不是包装器:
public interface IDummy { string DoSomething(); } public class Dummy: IDummy { public virtual string DoSomething() { return string.Empty; } } public class MyCustomException : Exception {} public class CustomIntercept: IInterceptor { public void Intercept(IInvocation invocation) { throw new MyCustomException(); } } internal class Program { private static void Main(string[] args) { var pg = new proxygenerator(); GetValue(pg.CreateInterfaceProxyWithoutTarget<IDummy>(new CustomIntercept())); GetValue(pg.CreateClassproxy<Dummy>(new CustomIntercept())); GetValue(pg.CreateClassproxyWithTarget<Dummy>(new Dummy(),new CustomIntercept())); GetValue(pg.CreateInterfaceProxyWithTarget<IDummy>(new Dummy(),new CustomIntercept())); } private static void GetValue(IDummy dummy) { try { dummy.DoSomething(); } catch (Exception e) { Console.WriteLine(e.GetType().Name); } } }
所有四个输出都是MyCustomException
你能确保TargetInvocationException不是来自你自己的代码吗?您正在使用什么版本的DynamicProxy(我正在使用Castle.Core 3.2中的那个版本)