PostSharp是一个.NEt下AOP的轻量级开源框架,它通过在MSBuild后注入IL的方式,现实代码的注入.
一个示例:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PostSharp.Laos;
using PostSharp;
namespace PostSharpApp
{
[MyTrace(AttributeTargetMembers="Test*")]
class Program
{
static void Main(string[] args)
{
Test1();
Test2();
Foo();
Console.ReadLine();
}
static void Test1()
{
Console.WriteLine("Hello world! 1");
}
static void Test2()
{
Console.WriteLine("Hello world! 2");
}
static void Foo()
{
Console.WriteLine("Hello world! 3");
}
}
[Serializable]
class MyTrace : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionEventArgs eventArgs)
{
Console.WriteLine("Entering {0}
![](https://www.cnblogs.com/Images/dot.gif)
}
public override void OnExit(MethodExecutionEventArgs eventArgs)
{
Console.WriteLine("Exiting
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
}
}
}
运行结果: