Exemple #1
0
        public ISetup Create(Expression <Action> expression)
        {
            MethodCallInfo methodCall = _expressionHelper.GetMethod(expression);

            if (methodCall == null)
            {
                throw new ArgumentException("Not a method", "expression");
            }

            var setup = new Setup(methodCall);

            AddSetup(setup);

            return(setup);
        }
        private IEnumerable <SetupTarget> GetSetupsFromInstructions(object target, MethodBody body, List <MethodDefinition> setupMethods)
        {
            foreach (var instruction in body.Instructions)
            {
                if (instruction.OpCode != OpCodes.Callvirt && instruction.OpCode != OpCodes.Call)
                {
                    continue;
                }

                var operandMethod = ((MethodReference)instruction.Operand).Resolve();

                if (setupMethods.Contains(operandMethod))
                {
                    Expression expression = _expressionDecompiler.Decompile(body, instruction, target);

                    if (expression == null)
                    {
                        // This should not happen. When this happens it's most likely
                        // a bug in the expression decompiler.
                        throw new SetupExtractionException("Could not extract expression");
                    }

                    var methodCall = _expressionHelper.GetMethod(expression);

                    if (methodCall == null)
                    {
                        string message = string.Format(
                            "Could not extract method from expression {0}", expression);
                        throw new SetupExtractionException(message);
                    }

                    yield return(new SetupTarget(expression, methodCall.Method));
                }
                else
                {
                    bool inSameClass = InSameClass(operandMethod.DeclaringType, body.Method.DeclaringType);
                    if (inSameClass && operandMethod != body.Method && operandMethod.Body != null)
                    {
                        foreach (var setup in GetSetupsFromInstructions(target, operandMethod.Body, setupMethods))
                        {
                            yield return(setup);
                        }
                    }
                }
            }
        }
Exemple #3
0
 public static MethodBase GetMethod(Expression <Action> expression)
 {
     return(ExpressionHelper.GetMethod(expression).Method);
 }