1 // **************************************************************** 2 // Copyright 2007, Charlie Poole 3 // This is free software licensed under the NUnit license. You may 4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4 5 // **************************************************************** 6 7 using System; 8 using System.Collections; 9 using System.Runtime.Remoting.Proxies; 10 using System.Runtime.Remoting.Messaging; 11 using System.Reflection; 12 13 namespace NUnit.Mocks 14 { 15 /// <summary> 16 /// Summary description for MockInterfaceHandler. 17 /// </summary> 18 public class MockInterfaceHandler : RealProxy 19 { 20 private ICallHandler callHandler; 21 MockInterfaceHandler( Type type, ICallHandler callHandler )22 public MockInterfaceHandler( Type type, ICallHandler callHandler ) : base( type ) 23 { 24 this.callHandler = callHandler; 25 } 26 Invoke( IMessage msg )27 public override IMessage Invoke( IMessage msg ) 28 { 29 IMethodCallMessage call = (IMethodCallMessage)msg; 30 IMethodReturnMessage result = null; 31 32 if ( call != null ) 33 { 34 try 35 { 36 object ret = callHandler.Call( call.MethodName, call.Args ); 37 38 if ( ret == null ) 39 { 40 MethodInfo info = call.MethodBase as MethodInfo; 41 Type returnType = info.ReturnType; 42 43 if( returnType == typeof( System.Boolean ) ) ret = false; 44 45 if( returnType == typeof( System.Byte ) ) ret = (System.Byte)0; 46 if( returnType == typeof( System.SByte ) ) ret = (System.SByte)0; 47 if( returnType == typeof( System.Decimal ) ) ret = (System.Decimal)0; 48 if( returnType == typeof( System.Double ) ) ret = (System.Double)0; 49 if( returnType == typeof( System.Single ) ) ret = (System.Single)0; 50 if( returnType == typeof( System.Int32 ) ) ret = (System.Int32)0; 51 if( returnType == typeof( System.UInt32 ) ) ret = (System.UInt32)0; 52 if( returnType == typeof( System.Int64 ) ) ret = (System.Int64)0; 53 if( returnType == typeof( System.UInt64 ) ) ret = (System.UInt64)0; 54 if( returnType == typeof( System.Int16 ) ) ret = (System.Int16)0; 55 if( returnType == typeof( System.UInt16 ) ) ret = (System.UInt16)0; 56 57 if( returnType == typeof( System.Char ) ) ret = '?'; 58 } 59 60 result = new ReturnMessage( ret, null, 0, null, call ); 61 } 62 catch( Exception e ) 63 { 64 result = new ReturnMessage( e, call ); 65 } 66 } 67 68 return result; 69 } 70 } 71 } 72 73