Пример #1
0
        public static ITranslation For(MethodCallExpression methodCall, ITranslationContext context)
        {
            var method     = new BclMethodWrapper(methodCall.Method);
            var parameters = new ParameterSetTranslation(method, methodCall.Arguments, context);

            if (context.Settings.ConvertPropertyMethodsToSimpleSyntax && IsDirectPropertyMethodCall(methodCall, out var propertyInfo))
            {
                var objTranslation = context.GetTranslationFor(methodCall.Object);
                var propertyAccess = new MemberAccessTranslation(objTranslation, propertyInfo.Name, propertyInfo.PropertyType);

                var isAssignment = methodCall.Method.ReturnType == typeof(void);
                if (isAssignment)
                {
                    var valueExpr = methodCall.Arguments.First();
                    return(new AssignmentTranslation(Assign, propertyAccess, valueExpr, context));
                }
                else
                {
                    return(propertyAccess);
                }
            }

            if (IsStringConcatCall(methodCall))
            {
                return(new StringConcatenationTranslation(Call, methodCall.Arguments, context));
            }

            if (methodCall.Method.IsImplicitOperator())
            {
                return(new CodeBlockTranslation(parameters[0]).WithNodeType(Call));
            }

            var subject = GetSubjectTranslation(methodCall, context);

            if (IsIndexedPropertyAccess(methodCall))
            {
                return(new IndexAccessTranslation(subject, parameters, methodCall.Type));
            }

            parameters = parameters.WithParentheses();

            if (methodCall.Method.IsExplicitOperator())
            {
                return(CastTranslation.ForExplicitOperator(
                           parameters[0],
                           context.GetTranslationFor(methodCall.Method.ReturnType)));
            }

            var methodCallTranslation = new StandardMethodCallTranslation(Call, subject, method, parameters, context);

            if (context.IsPartOfMethodCallChain(methodCall))
            {
                methodCallTranslation.AsPartOfMethodCallChain();
            }

            return(methodCallTranslation);
        }
Пример #2
0
        public static ITranslation For(MethodCallExpression methodCall, ITranslationContext context)
        {
            if (methodCall.Method.IsPropertyGetterOrSetterCall(out var property))
            {
                var getterTranslation = new PropertyGetterTranslation(methodCall, property, context);

                if (methodCall.Method.ReturnType != typeof(void))
                {
                    return(getterTranslation);
                }

                return(new PropertySetterTranslation(methodCall, getterTranslation, context));
            }

            if (IsStringConcatCall(methodCall))
            {
                return(new StringConcatenationTranslation(Call, methodCall.Arguments, context));
            }

            var method     = new BclMethodWrapper(methodCall.Method);
            var parameters = new ParameterSetTranslation(method, methodCall.Arguments, context);

            if (methodCall.Method.IsImplicitOperator())
            {
                return(new CodeBlockTranslation(parameters[0]).WithNodeType(Call));
            }

            var subject = GetSubjectTranslation(methodCall, context);

            if (IsIndexedPropertyAccess(methodCall))
            {
                return(new IndexAccessTranslation(subject, parameters, methodCall.Type));
            }

            parameters = parameters.WithParentheses();

            if (methodCall.Method.IsExplicitOperator())
            {
                return(CastTranslation.ForExplicitOperator(
                           parameters[0],
                           context.GetTranslationFor(methodCall.Method.ReturnType)));
            }

            var methodCallTranslation = new StandardMethodCallTranslation(Call, subject, method, parameters, context);

            if (context.IsPartOfMethodCallChain(methodCall))
            {
                methodCallTranslation.AsPartOfMethodCallChain();
            }

            return(methodCallTranslation);
        }