AOPの最適注入方式——MSIL注入

4072 ワード

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 + "\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);
        }
    }
}