有个接口
interface
I牛粪
{
void 吃();
}
{
void 吃();
}
有个接口的实现
class
牛粪 : I牛粪
{
public void 吃()
{
Console.WriteLine( " 吃 牛粪 拉! " );
}
}
{
public void 吃()
{
Console.WriteLine( " 吃 牛粪 拉! " );
}
}
我现在需要在这个吃的动作周围加上一大堆的鲜花,还有围观的群众。如果使用传统的编程方法,得到:
代码
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( " 鲜花 " );
}
}
{
牛粪 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( " 鲜花 " );
}
}
如果要还有添加”喝 “,“拉”之类的,怎么办?如果不同动作的入口参数也不一样,怎么办?重复的方法不能写很多次。于是使用代理基础。
代码
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();
}
{
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();
}
最后调用是:
代码
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( " 鲜花 " );
}
}
{
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( " 鲜花 " );
}
}
不知道这个技术算不算新。