基于netcore的微服务——AspectCore(AOP框架)(3)
AspectCore框架
一、示例代码
项目引入包
<PackageReference Include="AspectCore.Core" Version="0.5.0" />
1.执行方法
public class Person
{
[Customlnterceptor]
public virtual void Say(string name)
{
Console.WriteLine($"你好,我是{name}");
}
}
2.特性
class CustomlnterceptorAttribute:AbstractInterceptorAttribute
{
public async override Task Invoke(AspectContext context,AspectDelegate next)
{
Console.WriteLine("执行之前");
await next(context);//执行被拦截得方法
Console.WriteLine("执行之后");
}
3.Program.cs
static void Main(string[] args)
{
ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder();
using(IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build())
{
Person p = proxyGenerator.CreateClassProxy<Person>();
p.Say("张三");
}
Console.ReadKey();
}

二、解析
1.传入得Context内容

2.内容解析

3.大致实现过程


浙公网安备 33010602011771号