private Type BuildType() { AssemblyName aName = new AssemblyName("DynamicMockerFor" + klass.Name); AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly( aName, AssemblyBuilderAccess.RunAndSave); // For a single-module assembly, the module name is usually // the assembly name plus an extension. ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll"); TypeBuilder tb = mb.DefineType( "Mock" + klass.Name, TypeAttributes.Public, typeof(DynamicMockerBase), //list of interfaces implemented by the type new Type[] { klass } ); DefineCtor(tb); foreach (MethodInfo mi in MethodInfoCache.GetCachedMethods(klass)) { DefineInterfaceMethods(tb, mi); } // Finish the type. Type t = tb.CreateType(); // Saves the single-module assembly. ab.Save(aName.Name + ".dll"); return(t); }
public MockMethod(Type type, string name) { this.klass = type; this.Name = name; this.methods = MethodInfoCache.FindMethod(klass, name); if (methods.Count() == 0) { throw new ArgumentException("There is no method " + name + " in type " + type); } this.results = new Dictionary <object[], object>(); }
public MockMethod With(params object[] args) { if (this.args != null) { throw new InvalidOperationException("You already called With() !!!! Cannot call it twice without calling Return() first!"); } if (meth == null) { this.meth = MethodInfoCache.FindMethod(klass, Name, args.Select(a => a.GetType()).ToArray()); } this.args = args; return(this); }