下载PostSharp(Visual Studio Gallery)。
安装一个AOP编译器和引入PostSharp.Aspects(注意安装过程中请使用免费的Express版本),然后初步演示代码:
using PostSharp.Aspects; using System; namespace ConsoleApplication1 { [Serializable] public class AOPILTestAttribute : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { Console.WriteLine("进入方法:"+args.Method.Name); var argumentList = args.Arguments; var arguments = args.Method.GetParameters(); for (int i = 0; i <arguments.Length; i++) { Console.WriteLine("参数名称:" + arguments[i].Name + " 参数类型:" + arguments[i].ParameterType + " 参数数值:" + argumentList[i]); } } } public class MainTest { public event Action<int> MyAction = null; public void Call() { MyAction(1); } [AOPILTest] static void Main(string[] args) { Console.WriteLine("我是Main方法"); MainTest mt = new MainTest(); mt.MyAction += mt_MyAction; mt.Call(); } [AOPILTest] static void mt_MyAction(int i) { Console.WriteLine("我是Action,我的数值是:"+i); } } }