PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/mocks/MockInterfaceHandler.cs

#
C# | 72 lines | 52 code | 12 blank | 8 comment | 30 complexity | 24d87a5a1a48c9933baf4b4a8f6b28b7 MD5 | raw file
Possible License(s): GPL-2.0
  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
  5. // ****************************************************************
  6. using System;
  7. using System.Collections;
  8. using System.Runtime.Remoting.Proxies;
  9. using System.Runtime.Remoting.Messaging;
  10. using System.Reflection;
  11. namespace NUnit.Mocks
  12. {
  13. /// <summary>
  14. /// Summary description for MockInterfaceHandler.
  15. /// </summary>
  16. public class MockInterfaceHandler : RealProxy
  17. {
  18. private ICallHandler callHandler;
  19. public MockInterfaceHandler( Type type, ICallHandler callHandler ) : base( type )
  20. {
  21. this.callHandler = callHandler;
  22. }
  23. public override IMessage Invoke( IMessage msg )
  24. {
  25. IMethodCallMessage call = (IMethodCallMessage)msg;
  26. IMethodReturnMessage result = null;
  27. if ( call != null )
  28. {
  29. try
  30. {
  31. object ret = callHandler.Call( call.MethodName, call.Args );
  32. if ( ret == null )
  33. {
  34. MethodInfo info = call.MethodBase as MethodInfo;
  35. Type returnType = info.ReturnType;
  36. if( returnType == typeof( System.Boolean ) ) ret = false;
  37. if( returnType == typeof( System.Byte ) ) ret = (System.Byte)0;
  38. if( returnType == typeof( System.SByte ) ) ret = (System.SByte)0;
  39. if( returnType == typeof( System.Decimal ) ) ret = (System.Decimal)0;
  40. if( returnType == typeof( System.Double ) ) ret = (System.Double)0;
  41. if( returnType == typeof( System.Single ) ) ret = (System.Single)0;
  42. if( returnType == typeof( System.Int32 ) ) ret = (System.Int32)0;
  43. if( returnType == typeof( System.UInt32 ) ) ret = (System.UInt32)0;
  44. if( returnType == typeof( System.Int64 ) ) ret = (System.Int64)0;
  45. if( returnType == typeof( System.UInt64 ) ) ret = (System.UInt64)0;
  46. if( returnType == typeof( System.Int16 ) ) ret = (System.Int16)0;
  47. if( returnType == typeof( System.UInt16 ) ) ret = (System.UInt16)0;
  48. if( returnType == typeof( System.Char ) ) ret = '?';
  49. }
  50. result = new ReturnMessage( ret, null, 0, null, call );
  51. }
  52. catch( Exception e )
  53. {
  54. result = new ReturnMessage( e, call );
  55. }
  56. }
  57. return result;
  58. }
  59. }
  60. }