/// <summary> /// /// </summary> /// <param name="supportedInterface">interface which the stub /// class will support</param> /// <param name="stubMaker">object which will generate /// method bodies</param> /// <returns>new stub class</returns> public Type MakeStubClass(Type supportedInterface, IStubMaker stubMaker) { if ( ! supportedInterface.IsInterface ) { throw new ArgumentException( "Can only stub interfaces.", "supportedInterface" ); } TypeBuilder typeBuilder = _moduleBuilder.DefineType( "ProviderStub", TypeAttributes.Public, null, new Type[] { supportedInterface } ); defineAndImplementStubMethods(typeBuilder, supportedInterface, stubMaker); Type stubClass = typeBuilder.CreateType(); return stubClass; }
/// <summary> /// Define stub methods through <see cref="IStubMaker"/> /// </summary> /// <param name="typeBuilder">where to add methods</param> /// <param name="supportedInterface">interface which the stub /// class will support</param> /// <param name="stubMaker">object which will generate /// method bodies</param> private static void defineAndImplementStubMethods( TypeBuilder typeBuilder, Type supportedInterface, IStubMaker stubMaker ) { ArrayList methods = new ArrayList(); getMethodsForInterface(supportedInterface, methods); foreach (MethodInfo mi in methods) { MethodBuilder methodBuilder = typeBuilder.DefineMethod( mi.Name, MethodAttributes.Public | MethodAttributes.Virtual, mi.ReturnType, getParameterTypes(mi) ); ILGenerator ilg = methodBuilder.GetILGenerator(); stubMaker.ImplementStubMethod(ilg, mi); ilg.Emit(OpCodes.Ret); } }
private Type getDynamicImplementationType() { StubClassMaker classMaker = new StubClassMaker(); IStubMaker stubMaker = null; Assembly assembly = null; if ((assembly = _linker.LoadAssembly(NUNIT_ASSEMBLY_NAME)) != null) { stubMaker = new NUnitStubMaker(assembly, _linker); } else if ((assembly = _linker.LoadAssembly(MBUNIT_ASSEMBLY_NAME)) != null) { stubMaker = new MbUnitStubMaker(assembly, _linker); } else if ((assembly = _linker.LoadAssembly(CSUNIT_ASSEMBLY_NAME)) != null) { stubMaker = new csUnitStubMaker(assembly, _linker); } else if ((assembly = _linker.LoadAssemblyWithPartialName(NUNIT_ASSEMBLY_NAME)) != null) { stubMaker = new NUnitStubMaker(assembly, _linker); } else if ((assembly = _linker.LoadAssemblyWithPartialName(MBUNIT_ASSEMBLY_NAME)) != null) { stubMaker = new MbUnitStubMaker(assembly, _linker); } else if ((assembly = _linker.LoadAssemblyWithPartialName(CSUNIT_ASSEMBLY_NAME)) != null) { stubMaker = new csUnitStubMaker(assembly, _linker); } else { return(null); } Type stubClass = classMaker.MakeStubClass( typeof(ITestFramework), stubMaker ); return(stubClass); }
/// <summary> /// /// </summary> /// <param name="supportedInterface">interface which the stub /// class will support</param> /// <param name="stubMaker">object which will generate /// method bodies</param> /// <returns>new stub class</returns> public Type MakeStubClass(Type supportedInterface, IStubMaker stubMaker) { if (!supportedInterface.IsInterface) { throw new ArgumentException( "Can only stub interfaces.", "supportedInterface" ); } TypeBuilder typeBuilder = _moduleBuilder.DefineType( "ProviderStub", TypeAttributes.Public, null, new Type[] { supportedInterface } ); defineAndImplementStubMethods(typeBuilder, supportedInterface, stubMaker); Type stubClass = typeBuilder.CreateType(); return(stubClass); }