Example #1
0
        public MockMethod When(string name)
        {
            MockMethod m = new MockMethod(klass, name);

            ms.Add(m);
            return(m);
        }
Example #2
0
        public MockMethod When(string name)
        {
            MockMethod m;

            if (!ms.TryGetValue(name, out m))
            {
                m = new MockMethod(klass, name);
                ms.Add(name, m);
            }
            return(m);
        }
Example #3
0
        private void DefineMockMethod(TypeBuilder tb, MockMethod mockMethod)
        {
            MethodInfo mi = mockMethod.Method;

            Type[]        argTypes = ToTypeArray(mockMethod.Method.GetParameters());
            MethodBuilder mb       = tb.DefineMethod(mi.Name,
                                                     MethodAttributes.Virtual | MethodAttributes.Public | MethodAttributes.ReuseSlot | MethodAttributes.HideBySig,
                                                     mi.ReturnType,
                                                     argTypes);

            ILGenerator il = mb.GetILGenerator();

            LocalBuilder V_0 = il.DeclareLocal(typeof(MethodInfo));
            LocalBuilder V_1;

            if (mi.ReturnType != typeof(void))
            {
                V_1 = il.DeclareLocal(mi.ReturnType);
            }
            LocalBuilder V_2 = il.DeclareLocal(typeof(Type[]));
            LocalBuilder V_3 = il.DeclareLocal(typeof(object[]));

            Label afterjump = il.DefineLabel();

            il.Emit(OpCodes.Ldtoken, tb.TypeToken.Token);
            il.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle"));
            il.Emit(OpCodes.Ldstr, mi.Name);
            if (mi.ReturnType != typeof(void))
            {
                il.Emit(OpCodes.Ldc_I4, argTypes.Length);
                il.Emit(OpCodes.Newarr, typeof(Type));
                il.Emit(OpCodes.Stloc_2);
                il.Emit(OpCodes.Ldloc_2);

                for (int i = 0; i < argTypes.Length; i++)
                {
                    il.Emit(OpCodes.Ldc_I4, i);
                    il.Emit(OpCodes.Ldtoken, argTypes[i]);
                    il.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle"));
                    il.Emit(OpCodes.Stelem_Ref);
                    il.Emit(OpCodes.Ldloc_2);
                }
            }
            else
            {
                il.Emit(OpCodes.Ldsfld, typeof(Type).GetField("EmptyTypes"));
            }


            il.Emit(OpCodes.Call, typeof(Type).GetMethod("GetMethod", new Type[] { typeof(string), typeof(Type[]) }));
            il.Emit(OpCodes.Stloc_0);

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Call, typeof(DynamicMockerBase).GetMethod("RetrieveMethod", new Type[] { typeof(MethodInfo) }));
            il.Emit(OpCodes.Stloc_0);
            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Ldc_I4, argTypes.Length);
            il.Emit(OpCodes.Newarr, typeof(object));

            if (mi.ReturnType != typeof(void))
            {
                il.Emit(OpCodes.Stloc_3);
            }

            for (int i = 0; i < argTypes.Length; i++)
            {
                il.Emit(OpCodes.Ldloc_3);
                il.Emit(OpCodes.Ldc_I4, i);
                il.Emit(OpCodes.Ldarg, i + 1);
                if (argTypes[i].IsValueType)
                {
                    il.Emit(OpCodes.Box, argTypes[i]);
                }
                il.Emit(OpCodes.Stelem_Ref);
            }
            if (mi.ReturnType != typeof(void))
            {
                il.Emit(OpCodes.Ldloc_3);
            }
            MethodInfo cm = typeof(MockMethod).GetMethod("Call", new Type[] { typeof(object[]) });

            il.Emit(OpCodes.Callvirt, cm);

            if (mi.ReturnType == typeof(void))
            {
                il.Emit(OpCodes.Pop);
            }
            else if (mi.ReturnType.IsValueType && !cm.ReturnType.IsValueType)
            {
                il.Emit(OpCodes.Unbox_Any, mi.ReturnType);
            }
            else
            {
                il.Emit(OpCodes.Castclass, mi.ReturnType);
            }
            il.Emit(OpCodes.Ret);
        }