.NET中Castle实现AOP

Castle

第一种

 解决方案.引用Castle.Core4.4.0

Program.cs

using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AopWhitCastleDynamicProxy2
{
    class Program
    {
        static void Main(string[] args)
        {
            ProxyGenerator generator = new ProxyGenerator();
            PersonInterceptor interceptor = new PersonInterceptor();

            //使用【代理类生成器】创建Person对象,而不是使用new关键字来实例化  
            Person person = generator.CreateClassProxy<Person>(interceptor);

            Console.WriteLine("当前类型:{0},父类型:{1}", person.GetType(), person.GetType().BaseType);
            Console.WriteLine();

            person.SayHello();//跟普通调用没有两样吧?  
            Console.WriteLine();

            person.SayName("福建");//跟普通调用没有两样吧?  
            Console.WriteLine();

            person.SayOther();//它不是虚方法,无法拦截。待会检测输出情况就知道了。     
            Console.ReadLine();

            Console.WriteLine();
        }
    }
}

Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AopWhitCastleDynamicProxy2
{
    public class Person
    {

        public virtual void SayHello()
        {
            Console.WriteLine("您好!");
        }

        public virtual void SayName(string hometown)
        {
            Console.WriteLine($"我是天涯人,我来自:{hometown}");
        }

        public void SayOther()
        {
            Console.WriteLine("我是中国人。");
        }
    }
}

PersonInterceptor.cs

using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AopWhitCastleDynamicProxy2
{
   
        internal class PersonInterceptor : StandardInterceptor
        {

            protected override void PreProceed(IInvocation invocation)
            {
                Console.WriteLine($"调用前的拦截器,方法名是:{invocation.Method.Name}");
                base.PreProceed(invocation);
            }

            protected override void PerformProceed(IInvocation invocation)
            {
                Console.WriteLine($"拦截的方法返回时调用的拦截器,方法名是:{invocation.Method.Name}");
                base.PerformProceed(invocation);
            }

            protected override void PostProceed(IInvocation invocation)
            {
                Console.WriteLine($"调用后的拦截器,方法名是:{invocation.Method.Name}");
                base.PostProceed(invocation);
            }
        }
    
}

 第二种

解决方案.引用Castle.Core4.4.0

Program.cs

using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AopWhitCastleDynamicProxy1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyIntercept myIntercept = new MyIntercept();//实例化拦截器
            ProxyGenerator proxy = new ProxyGenerator(); //实例化代理
            ITestIntercept intercept = proxy.CreateInterfaceProxyWithTarget<ITestIntercept>(new TestIntercept(), myIntercept);
            intercept.Test("william");
            Console.ReadLine();
        }
    }
}

ITestIntercept.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AopWhitCastleDynamicProxy1
{
    public interface ITestIntercept
    {
        string Test(string p);
    }
}

TestIntercept.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AopWhitCastleDynamicProxy1
{
    public class TestIntercept : ITestIntercept
    {
        public string Test(string p)
        {
            throw new Exception("异常了"); //演示抛出异常,拦截器是否能捕捉到异常信息
                                        //return p;

        }
    }
}

MyIntercept.cs

using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace AopWhitCastleDynamicProxy1
{
    class MyIntercept : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            Console.WriteLine("【进入拦截器】");
            MethodInfo method = invocation.GetConcreteMethod();//得到被拦截的方法
            var parameter = invocation.Arguments[0].ToString();//获取被拦截的方法参数
            if (!invocation.MethodInvocationTarget.IsAbstract)
            {
                Console.WriteLine("【被拦截的方法执行前】" + method.Name + "的参数" + parameter);

                try
                {
                    invocation.Proceed();
                }
                catch (Exception ex)
                {

                    Console.WriteLine("【拦截到异常】" + ex.Message);
                }
                Console.WriteLine("【被拦截的方法执结果】" + invocation.ReturnValue);

            }
            Console.WriteLine("【被拦截的方法执完毕】");
        }
    }
}
原文地址:https://www.cnblogs.com/wangyinlon/p/12792848.html