PostSharp是一个.NEt下AOP的轻量级开源框架,它通过在MSBuild后注入IL的方式,现实代码的注入.
官方网站:http://www.postsharp.org
一个示例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
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}
", eventArgs.Method);
}
public override void OnExit(MethodExecutionEventArgs eventArgs)
{
Console.WriteLine("Exiting
{0}
", eventArgs.Method);
}
}
}
运行结果:
![](https://www.cnblogs.com/images/cnblogs_com/rockniu/postsharp.GIF)