切面编程AOP之KingAOP

时间:2023-03-09 06:34:29
切面编程AOP之KingAOP

1. 在Nuget上安装KingAOP

2. 创建一个新的类

public class Test : IDynamicMetaObjectProvider
{
public DynamicMetaObject GetMetaObject(Expression paramter)
{
return new AspectWeaver(paramter, this);
} [LogAspec]
public void Operate()
{
Console.WriteLine("Call 'Test类' - ‘Operate方法’");
}
} public class LogAspec : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
Console.WriteLine("OnEntry: Hello KingAOP");
} public override void OnException(MethodExecutionArgs args)
{
Console.WriteLine("OnException: Hello KingAOP");
} public override void OnSuccess(MethodExecutionArgs args)
{
Console.WriteLine("OnSuccess: Hello KingAOP");
} public override void OnExit(MethodExecutionArgs args)
{
Console.WriteLine("OnExit: Hello KingAOP");
}
}

  

3 在控制台中调用该类 (实践后发现,如果需要用KingAop进行横向切面则必须在实例化切面的类时用动态类型dynamic接收)

static void Main(string[] args)
{
#region kingAOP
dynamic test = new Test();
try
{
Console.WriteLine("Call Mian ..");
test.Operate();
Console.WriteLine("Exit Main ..");
}
catch (Exception ex)
{
throw ex;
}
Console.Read();
#endregion
}