首页 > 代码库 > AOP面向切面编程

AOP面向切面编程

 

 

首先是下载准备好三个dll类库

Microsoft.Practices.EnterpriseLibrary.PolicyInjection.dll

Microsoft.Practices.Unity.dll

Microsoft.Practices.Unity.Interception.dll

 

第二步、将三个dll文件,包含进项目的DLL类库文件夹,然后在项目中引用它们三个。

 

第三步、定义一个Handler类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace Aop
{
    public class Handler : ICallHandler
    {
        public int Order { get; set; }
        public string MethodName { get; set; }

// input是方法的参数,getNext是要执行的方法
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { Console.WriteLine("拦截方法前的逻辑" + MethodName); var inputs = input.Inputs; if (inputs.Count > 0) { for (int i = 0; i < inputs.Count; i++) { Console.WriteLine("参数:" + Serializer.ToJson(inputs[i])); } } var returnmsg = getNext()(input, getNext); return returnmsg; } } }

 2)顶一个一个HandlerAttr.cs类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.Unity;

namespace Aop
{
    public class HandlerAttr : HandlerAttribute
    {
        public string MethodName { get; set; }

        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new Handler() { MethodName = this.MethodName };
        }
    }
}

 3)一个其他的帮助类,提前已经准备好了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Aop
{
    /// <summary>
    /// 强类型JSON序列化帮助类
    /// </summary>
    public class Serializer
    {
        /// <summary>
        /// 序列化目标对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象实例</param>
        /// <returns>Json字符串</returns>
        public static string ToJson<T>(T obj)
        {
            if (obj == null) return string.Empty;
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, obj);
            string retVal = System.Text.Encoding.UTF8.GetString(ms.ToArray());
            ms.Dispose();
            return retVal;
        }

        /// <summary>
        /// 反序列化json字符为对象实例
        /// </summary>
        /// <typeparam name="T">目标对象类型</typeparam>
        /// <param name="json">json字符串</param>
        /// <returns>对象实例</returns>
        public static T ToObject<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
            obj = (T)serializer.ReadObject(ms);
            ms.Close();
            ms.Dispose();
            return obj;
        }
    }
}

4)给一个方法标记上特性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Aop
{
    public class Parm {
        public string Key { get; set; }
    }

    public interface ITest
    {
        void Method(string str);
        void Method2(Parm p);
    }

    public class Test : MarshalByRefObject, ITest
    {
        [HandlerAttr(MethodName = "Method注解")]
        public void Method(string str)
        {
            Console.WriteLine(string.Format("这是Method(),参数是:{0}", str));
        }

        [HandlerAttr(MethodName = "Method2注解")]
        public void Method2(Parm p)
        {
            Console.WriteLine("这是Method2()");
        }
    }
}

 

5)调用执行方法,查看切面代码是否执行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;

namespace Aop
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Test test = PolicyInjection.Create<Test>();
                test.Method("A");

                test.Method2(new Parm() { Key = "A" });
            }
            catch (Exception ex)
            {
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}

 

  

 

AOP面向切面编程