下載PostSharp(Visual Studio Gallery)。ide
安裝一個AOP編譯器和引入PostSharp.Aspects(注意安裝過程當中請使用免費的Express版本),而後初步演示代碼:spa
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 + "\t參數類型:" + arguments[i].ParameterType + "\t參數數值:" + 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); } } }