c# AOP 通过ContextBoundObject、ContextAttribute 上下文对象实现

通过
继承ContextBoundObject class
继承ContextAttribute 特性

使用方法:

/// 需要的地方:
/// 1.加 AOP 特性
/// 2.继承 ContextBoundObject

具体代码如下:

namespace MyAOP
{
    /// <summary>
    /// 需要的地方:
    /// 1.加 AOP 特性
    /// 2.继承 ContextBoundObject
    /// </summary>
    //必须加上aop特性才可以实现 
    [AOP]
    class ZTest : ContextBoundObject
    {
       // 继承 ContextBoundObject的所有子类,都会获取到上下文信息,

       /* 通过ContextBoundObject来实现AOP,
       * 其中有大约60%的时间花在GetCustomAttribute的反射上,
       * 另外40%则是MarshalByRefObject(ContextBoundObject的基类,
       * 实际上ContextBoundObject什么事情都没做,只是单纯的继承了MarshalByRefObject)
       * 内部上下文管理上,它需要管理些啥呢?ContextBoundObject是一个上下文对象的基类
       * ,继承自它的子类将被一个特别的上下文管理起来,这个上下文包括一系列的属性集合或者规则,
       * 当进入或者离开上下文时,将强制执行规则。当使用ContextBoundObject实现AOP时,除了性能底下,
       * 其必须继承自ContextBoundObject,对于单继承的语言来讲可能带来不便。
       */

        //think?好像static的函数不行?
        
        public  void Test1()
        {
            Console.WriteLine("do test1");
        }

        public int Test2()
        {
            Console.WriteLine("do test2");
            //throw new Exception("ee");
            return -100;
        }
    }


    class AOPAttribute : ContextAttribute, IContributeObjectSink
    {
        public AOPAttribute() : base("AOP") { }

        // IContributeObjectSink
        // :在远程处理调用的服务器端分配对象特定的侦听接收器,
        // 这个地方监听到AOPAttribute标记的对象时,会执行GetObjectSink方法

        public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
        {
            return new MessageSink(nextSink);
        }
    }



    /// <summary>
    /// 监听用的消息
    /// </summary>
    class MessageSink : IMessageSink
    {
        private IMessageSink _nextShik;
        public IMessageSink NextSink => _nextShik;

        public MessageSink(IMessageSink next)
        {
            this._nextShik = next;
        }

        /// <summary>
        /// 异步不用管
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="replySink"></param>
        /// <returns></returns>
        public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 以同步方式处理给定的消息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public IMessage SyncProcessMessage(IMessage msg)
        {
            //可以给方法加上自定义特性  那就可以从中取值

            //IMessage result = null;
            //var call = msg as IMethodCallMessage;
            //if (call == null || Attribute.GetCustomAttribute(call.MethodBase, typeof(DecoratorMethodAttribute)) == null)
            //{
            //    result = NextSink.SyncProcessMessage(msg);
            //}
            //else
            //{
            //    var attr = Attribute.GetCustomAttribute(call.MethodBase, typeof(DecoratorMethodAttribute)) as DecoratorMethodAttribute;
            //    Console.WriteLine(attr.PrefixPrint);
            //    result = NextSink.SyncProcessMessage(msg);
            //    Console.WriteLine(attr.SuffixPrint);
            //}

            //这里的strat 和 end 输出 可以用Action的方式 对外广播事件
            Console.WriteLine("start");
            // 执行方法,并返回msg
            var msg2 = _nextShik.SyncProcessMessage(msg);
            var returnMsg = msg2 as IMethodReturnMessage;

            //returnMsg.ReturnValue;  获取返回值
            //returnMsg.Exception    获取异常

            Console.WriteLine("end");
            return msg2;
        }
    }

}

结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值