|
From: Levi K. <lkh...@us...> - 2004-08-24 21:15:53
|
Update of /cvsroot/nmock/nmock/src/NMock/Proxy In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25016/src/NMock/Proxy Added Files: MockRealProxy.cs Log Message: Added ProxyMock: can mock interface and MarshalByRefObjects, can mock sealed classes/non-empty contructor and nonvirtual members; related refactoring; some MethodSignature methods moved to a new class --- NEW FILE: MockRealProxy.cs --- using System; using System.Reflection; using System.Runtime.Remoting; using System.Runtime.Remoting.Proxies; using System.Runtime.Remoting.Messaging; namespace NMock.Proxy { public class MockRealProxy : RealProxy { private readonly IMock mock; private readonly Type type; public MockRealProxy(IMock mock, Type type) : base(type) { this.mock = mock; this.type = type; } public override IMessage Invoke(IMessage msg) { IMethodCallMessage call = msg as IMethodCallMessage; if (call == null) { throw new NotSupportedException("Unsupported call type " + msg.GetType()); } if (call.MethodName == "GetType" && call.InArgCount == 0) { return new ReturnMessage(type, null, 0, null, call); } return new ReturnMessage(ExecuteCall(call), null, 0, null, call); } private object ExecuteCall(IMethodCallMessage call) { string methodName = TypeCheckedMock.StripGetSetPrefix(call.MethodName); object result = mock.Invoke(methodName, call.InArgs); if (result == null) { MethodInfo method = type.GetMethod(call.MethodName, (Type[])call.MethodSignature); if (method.ReturnType.IsValueType) { Console.WriteLine(method.ReturnType.FullName); result = Activator.CreateInstance(method.ReturnType); } } return result; } } } |