/// <summary>
/// 自定义AOP扩展
/// </summary>
public static class ContainerAOPExtensions
{
/// <summary>
///
/// </summary>
/// <param name="t">对象</param>
/// <param name="interfaceType">接口类型</param>
/// <returns></returns>
public static object AOP(this object t, Type interfaceType)
{
ProxyGenerator generator = new ProxyGenerator();
ProxyInterceptor interceptor = new ProxyInterceptor();
t = generator.CreateInterfaceProxyWithTarget(interfaceType, t, interceptor);
return t;
}
}
public abstract class BaseInterceptorAttribute : Attribute
{
public abstract Action Do(IInvocation invocation, Action action);
}
/// <summary>
/// 学习前吃点东西
/// </summary>
public class EatInterceptorAttribute : BaseInterceptorAttribute
{
public override Action Do(IInvocation invocation, Action action)
{
return () =>
{
Console.WriteLine($"This is Eat1 {invocation.Method.Name} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}");
//去执行真实逻辑
action.Invoke();
Console.WriteLine($"This is Eat2 {invocation.Method.Name} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}");
};
}
}
/// <summary>
/// 学习前上个厕所
/// </summary>
public class GoToiletInterceptorAttribute : BaseInterceptorAttribute
{
public override Action Do(IInvocation invocation, Action action)
{
return () =>
{
Console.WriteLine($"This is GoToilet1 {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}");
//去执行真实逻辑
action.Invoke();
Console.WriteLine($"This is GoToilet2 {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}");
};
}
}
/// <summary>
/// 拦截器
/// </summary>
public class ProxyInterceptor : StandardInterceptor
{
/// <summary>
/// 调用前的拦截器
/// </summary>
/// <param name="invocation"></param>
protected override void PreProceed(IInvocation invocation)
{
Console.WriteLine($"调用前的拦截器 方法名是:{invocation.Method.Name} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}");
}
/// <summary>
/// 拦截的方法返回时调用的拦截器
/// </summary>
/// <param name="invocation"></param>
protected override void PerformProceed(IInvocation invocation)
{
var method = invocation.Method;
//真实逻辑包成委托--Study()--对应学生服务里的‘我要学习了’
Action action = () => base.PerformProceed(invocation);
if (method.IsDefined(typeof(BaseInterceptorAttribute), true))
{
foreach (var attribute in method.GetCustomAttributes<BaseInterceptorAttribute>().ToArray().Reverse())
{
//组装、再组装委托
action = attribute.Do(invocation, action);
}
}
//最后一次性执行委托
action.Invoke();
}
/// <summary>
/// 调用后的拦截器
/// </summary>
/// <param name="invocation"></param>
protected override void PostProceed(IInvocation invocation)
{
Console.WriteLine($"调用后的拦截器 方法名是:{invocation.Method.Name} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}");
}
}
/// <summary>
/// 学生接口
/// </summary>
public interface IStudentService
{
/// <summary>
/// 学习
/// </summary>
[EatInterceptor]
[GoToiletInterceptor]
public void Study();
}
5.2、StudentService.cs
/// <summary>
/// 学生服务
/// </summary>
public class StudentService : IStudentService
{
/// <summary>
/// 学习
/// </summary>
public void Study()
{
Console.WriteLine("我要学习了!");
}
}
public class TestCustomAOPController : Controller
{
//学生服务接口
private IStudentService _iStudentService = null;
public TestCustomAOPController(IStudentService iStudentService)
{
_iStudentService = iStudentService;
}
public IActionResult Index()
{
Console.WriteLine("******************普通方法调用*********************");
_iStudentService.Study();
Console.WriteLine("*******************AOP扩展后********************");
_iStudentService = (IStudentService)_iStudentService.AOP(typeof(IStudentService));
_iStudentService.Study();
return View();
}
}
【ASP.NET Core 3.1_参考中间件源码实现自定义AOP】
原文:https://www.cnblogs.com/menglin2010/p/13971506.html