请问,为一方法增加 IThrowsAdvice,在之方法里抛出异常,在vs中就抛出了,如何不让它抛出呢,而是在程序中打印出异常信息。spring.net aop中的example是没有在vs中抛出的。差不多一样的代码,是不是vs哪里有设置?    public class ServiceCommand : ICommand
    {
        #region ICommand Members        public bool IsUndoCapable
        {
            get { return true; }
        }        public void Execute()
        {
            Console.Out.WriteLine("---原生方法执行 ServiceCommand implementation : Execute()... ---");
            throw new Exception("my exception");
        }        public void UnExecute()
        {
            Console.Out.WriteLine("--- ServiceCommand implementation : UnExecute()... ---");            // Uncomment to see IThrowAdvice implementation in action
            //throw new Exception("The method or operation is not supported.");
        }        #endregion
    }/****************************************************/ static void Main(string[] args)
        {
            try
            {
                // Create AOP proxy programmatically.
                ProxyFactory factory = new ProxyFactory(new ServiceCommand());
                //factory.AddAdvice(new ConsoleLoggingBeforeAdvice());
                //factory.AddAdvice(new ConsoleLoggingAfterAdvice());                //factory.AddAdvice(new ConsoleLoggingAroundAdvice());                factory.AddAdvice(new ConsoleLoggingThrowsAdvice());
                ICommand command = (ICommand)factory.GetProxy();                command.Execute();    // //在此处就提示异常了,如何禁用?这里不是有捕获,可是还是出来了。                //if (command.IsUndoCapable)
                //{
                //    command.UnExecute();
                //}
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine();
                Console.Out.WriteLine(ex);
            }
            finally
            {
                Console.Out.WriteLine();
                Console.WriteLine("done");
                Console.Out.WriteLine("--- hit <return> to quit ---");
                Console.ReadLine();
            }
        }

解决方案 »

  1.   

    使用环绕通知IMethodInterceptor
    在Invoke中实现以下代码            try
                {
                    //执行
                    object obj = invocation.Proceed();
                    return obj;
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.ToString());  //打印异常
                    logger.Error(ex);  //log4net组件写入日志
                    return null;
                }
                finally
                {
                    //结束
                }