利用RealProxy,强化AOP,在”牛粪“的周围插上”鲜花“


有个接口

     interface  I牛粪
    {
        
void  吃();
    }

 

 

有个接口的实现

 

     class  牛粪 : I牛粪
    {
        
public   void  吃()
        {
            Console.WriteLine(
" 吃 牛粪 拉! " );
        }
    }

 

 

我现在需要在这个吃的动作周围加上一大堆的鲜花,还有围观的群众。如果使用传统的编程方法,得到:

ExpandedBlockStart.gif 代码
         public   void  Test001()
        {
            牛粪 sheet 
=   new  牛粪();

            
for  ( int  i  =   0 ; i  <   3 ; i ++ )
            {
                Console.WriteLine(
" 鲜花 " );
            }

            Console.WriteLine(
" 围观群众 " );

            sheet.吃();

            Console.WriteLine(
" 围观群众 " );

            
for  ( int  i  =   0 ; i  <   3 ; i ++ )
            {
                Console.WriteLine(
" 鲜花 " );
            }
        }

 

 

如果要还有添加”喝 “,“拉”之类的,怎么办?如果不同动作的入口参数也不一样,怎么办?重复的方法不能写很多次。于是使用代理基础。

ExpandedBlockStart.gif 代码
     class  Mock < T >  : RealProxy
    {
        T instance;
        
public   delegate   void  OnMock(IMockMethod method);
        
public   event  OnMock OnMockMethod;
        
class  MockMethod : IMockMethod
        {
            T instance;
            IMethodCallMessage methodCall;
            
object  result;
            
public  MockMethod(T instance, IMethodCallMessage methodCall)
            {
                
this .instance  =  instance;
                
this .methodCall  =  methodCall;
            }
            
public   void  Execute()
            {
                result 
=  methodCall.MethodBase.Invoke(instance, methodCall.InArgs);
            }
            
public   object  Result
            {
                
get
                {
                    
return  result;
                }
            }
        }

        
public  Mock(T instance)
            : 
base ( typeof (T))
        {
            
this .instance  =  instance;
        }

        
public   override  IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage methodCall 
=  msg  as  IMethodCallMessage;

            MockMethod mock 
=   new  MockMethod( this .instance, methodCall);

            
this .OnMockMethod(mock);

            
return   new  ReturnMessage(mock.Result,  null 0 null , methodCall);
        }
        
public  T Value
        {
            
get
            {
                
return  (T) this .GetTransparentProxy();
            }
        }
        
public   bool  Result
        {
            
get
            {
                
return   false ;
            }
        }
    }

    
interface  IMockMethod
    {
        
void  Execute();
    }

 

 

最后调用是:

ExpandedBlockStart.gif 代码
         public   void  test222()
        {
            Mock
< I牛粪 >  mock  =   new  Mock < I牛粪 > ( new  牛粪());
            mock.OnMockMethod 
+=   new  Mock < I牛粪 > .OnMock(mock_OnMockMethod);
            mock.Value.吃();
        }

        
void  mock_OnMockMethod(IMockMethod method)
        {
            
for  ( int  i  =   0 ; i  <   3 ; i ++ )
            {
                Console.WriteLine(
" 鲜花 " );
            }
            Console.WriteLine(
" 围观群众 " );
            method.Execute();
            Console.WriteLine(
" 围观群众 " );

            
for  ( int  i  =   0 ; i  <   3 ; i ++ )
            {
                Console.WriteLine(
" 鲜花 " );
            }
        }

 

 

不知道这个技术算不算新。 

 

转载于:https://www.cnblogs.com/zc22/archive/2010/03/03/1677451.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值