private static MethodCallPredicateCheck TryCreateImpl(IInvocationExpression invocationExpression,
                                                              bool hasNot)
        {
            var callSiteType = invocationExpression.GetCallSiteType();

            if (IsEnum(callSiteType))
            {
                return(EnumValidationPredicateCheck.TryCreateEnumPredicateCheck(invocationExpression, hasNot));
            }

            var method = invocationExpression.GetCalledMethod();

            var argument = ExtractArgument(invocationExpression.Arguments.FirstOrDefault().With(x => x.Value));

            argument = argument ?? new EmptyPredicateArgument();

            if (callSiteType == null || method == null)
            {
                return(null);
            }

            return(new MethodCallPredicateCheck(argument, callSiteType)
            {
                MethodName = method,
                _hasNot = hasNot,
            });
        }
Exemplo n.º 2
0
        private static CodeContractStatementType?GetContractAssertionType(IInvocationExpression invocationExpression)
        {
            var clrType = invocationExpression.GetCallSiteType();
            var method  = invocationExpression.GetCalledMethod();

            if (clrType.Return(x => x.FullName) != typeof(Contract).FullName)
            {
                return(null);
            }

            return(ParseCodeContractStatementType(method));
        }
Exemplo n.º 3
0
        private static IReferenceExpression ExtractContractResultReference(IInvocationExpression contractResultExpression)
        {
            Contract.Requires(contractResultExpression != null);

            var callSiteType = contractResultExpression.GetCallSiteType();
            var method       = contractResultExpression.GetCalledMethod();

            if (callSiteType.With(x => x.FullName) != typeof(Contract).FullName ||
                method != "Result")
            {
                return(null);
            }

            return(contractResultExpression
                   .With(x => x.InvokedExpression)
                   .Return(x => x as IReferenceExpression));
        }
Exemplo n.º 4
0
        public static EnumValidationPredicateCheck TryCreateEnumPredicateCheck(IInvocationExpression invocationExpression, bool hasNot)
        {
            Contract.Requires(invocationExpression != null);

            // Expected expression:
            // Enum.IsDefined(typeof(System.Reflection.BindingFlags), flags)

            var callSiteType = invocationExpression.GetCallSiteType();

            Contract.Assert(IsEnum(callSiteType));
            var method    = invocationExpression.GetCalledMethod();
            var arguments = invocationExpression.Arguments.ToList();

            if (arguments.Count != 2)
            {
                return(null);
            }

            // First argument is typeof(EnumType)
            var enumType =
                arguments[0].Value
                .With(x => x as ITypeofExpression)
                .With(x => x.ArgumentType)
                .With(x => x.GetClrTypeName());

            // Second argument is a enum value itself
            var argument = ExtractArgument(arguments[1].Value);

            if (argument == null)
            {
                return(null);
            }

            return(new EnumValidationPredicateCheck(argument, callSiteType)
            {
                CheckedEnumTypeName = enumType,
                MethodName = method,
                _hasNot = hasNot,
            });
        }
        private static MethodCallPredicateCheck TryCreateImpl(IInvocationExpression invocationExpression,
            bool hasNot)
        {
            var callSiteType = invocationExpression.GetCallSiteType();

            if (IsEnum(callSiteType))
                return EnumValidationPredicateCheck.TryCreateEnumPredicateCheck(invocationExpression, hasNot);

            var method = invocationExpression.GetCalledMethod();

            var argument = ExtractArgument(invocationExpression.Arguments.FirstOrDefault().With(x => x.Value));

            argument = argument ?? new EmptyPredicateArgument();

            if (callSiteType == null || method == null)
                return null;

            return new MethodCallPredicateCheck(argument, callSiteType)
            {
                MethodName = method,
                _hasNot = hasNot,
            };
        }
        public static EnumValidationPredicateCheck TryCreateEnumPredicateCheck(IInvocationExpression invocationExpression, bool hasNot)
        {
            Contract.Requires(invocationExpression != null);
            
            // Expected expression: 
            // Enum.IsDefined(typeof(System.Reflection.BindingFlags), flags)

            var callSiteType = invocationExpression.GetCallSiteType();

            Contract.Assert(IsEnum(callSiteType));
            var method = invocationExpression.GetCalledMethod();
            var arguments = invocationExpression.Arguments.ToList();

            if (arguments.Count != 2)
                return null;

            // First argument is typeof(EnumType)
            var enumType =
                arguments[0].Value
                    .With(x => x as ITypeofExpression)
                    .With(x => x.ArgumentType)
                    .With(x => x.GetClrTypeName());

            // Second argument is a enum value itself
            var argument = ExtractArgument(arguments[1].Value);

            if (argument == null)
                return null;

            return new EnumValidationPredicateCheck(argument, callSiteType)
            {
                CheckedEnumTypeName = enumType,
                MethodName = method,
                _hasNot = hasNot,
            };
        }
        private static CodeContractStatementType? GetContractAssertionType(IInvocationExpression invocationExpression)
        {
            var clrType = invocationExpression.GetCallSiteType();
            var method = invocationExpression.GetCalledMethod();

            if (clrType.Return(x => x.FullName) != typeof(Contract).FullName)
                return null;

            return ParseCodeContractStatementType(method);
        }