/// <summary> /// Generates a mock object with an interface defined by an array of method signatures. /// </summary> /// <param name="typeName">Name of the type to ganerate.</param> /// <param name="methodSignatures">Array of method signatures of methods to be implemented.</param> /// <param name="handler">Mocked calls handler.</param> /// <returns>Instance of generated class.</returns> public object Generate(string typeName, MethodSignature[] methodSignatures, IMockedCallHandler handler) { MockClassBuilder classBuilder = new MockClassBuilder( _moduleBuilder, typeName, typeof(object), Type.EmptyTypes ); foreach (MethodSignature ms in methodSignatures) { classBuilder.ImplementMockedMethod(ms.MethodName, ms.ReturnType, ms.ParamTypes); } return(compileAndGenerateMock(classBuilder, handler)); }
private object compileAndGenerateMock(MockClassBuilder classBuilder, IMockedCallHandler handler) { // create type classBuilder.Compile( ); object newMockObject = Activator.CreateInstance(classBuilder.MockClass); // set handler field classBuilder.HandlerField.SetValue(newMockObject, handler); // save if necessary if (_assemblyFilename != null) { _assemblyBuilder.Save(_assemblyFilename); } return(newMockObject); }
/// <summary> /// Generates a mock object for the specified type. /// </summary> /// <param name="type">type to generate proxy for.</param> /// <param name="handler"><see cref="IMockedCallHandler"/> which /// will handle all calls to the generated mock object.</param> /// <returns>proxy mock object for input type.</returns> public object Generate( Type type, IMockedCallHandler handler ) { string mockClassName = "Mock" + type.Name; Type superClass; Type[] interfaces; determineSuperClassAndInterfaces( type, out superClass, out interfaces ); MockClassBuilder classBuilder = new MockClassBuilder( _moduleBuilder, mockClassName, superClass, interfaces ); IList methods = getMethods( type ); foreach ( MethodInfo mi in methods ) { classBuilder.ImplementMockedMethod( mi ); } return compileAndGenerateMock( classBuilder, handler ); }
/// <summary> /// Generates a mock object for the specified type. /// </summary> /// <param name="type">type to generate proxy for.</param> /// <param name="handler"><see cref="IMockedCallHandler"/> which /// will handle all calls to the generated mock object.</param> /// <returns>proxy mock object for input type.</returns> public object Generate(Type type, IMockedCallHandler handler) { string mockClassName = "Mock" + type.Name; Type superClass; Type[] interfaces; determineSuperClassAndInterfaces( type, out superClass, out interfaces ); MockClassBuilder classBuilder = new MockClassBuilder( _moduleBuilder, mockClassName, superClass, interfaces ); IList methods = getMethods(type); foreach (MethodInfo mi in methods) { classBuilder.ImplementMockedMethod(mi); } return(compileAndGenerateMock(classBuilder, handler)); }
private object compileAndGenerateMock( MockClassBuilder classBuilder, IMockedCallHandler handler ) { // create type classBuilder.Compile( ); object newMockObject = Activator.CreateInstance( classBuilder.MockClass ); // set handler field classBuilder.HandlerField.SetValue( newMockObject, handler ); // save if necessary if ( _assemblyFilename != null ) { _assemblyBuilder.Save( _assemblyFilename ); } return newMockObject; }
/// <summary> /// Generates a mock object with an interface defined by an array of method signatures. /// </summary> /// <param name="typeName">Name of the type to ganerate.</param> /// <param name="methodSignatures">Array of method signatures of methods to be implemented.</param> /// <param name="handler">Mocked calls handler.</param> /// <returns>Instance of generated class.</returns> public object Generate( string typeName, MethodSignature[] methodSignatures, IMockedCallHandler handler ) { MockClassBuilder classBuilder = new MockClassBuilder( _moduleBuilder, typeName, typeof ( object ), Type.EmptyTypes ); foreach ( MethodSignature ms in methodSignatures ) { classBuilder.ImplementMockedMethod( ms.MethodName, ms.ReturnType, ms.ParamTypes ); } return compileAndGenerateMock( classBuilder, handler ); }