Ejemplo n.º 1
0
        private static string GetGeneratedCode(LanguageElement originalCall)
        {
            if (!(originalCall is IHasArguments))
            {
                return(null);
            }
            ExpressionCollection originalArguments = (originalCall as IHasArguments).Arguments;

            if (originalArguments == null)
            {
                return(null);
            }
            LanguageElement replacementCall = originalCall.Clone() as LanguageElement;

            (replacementCall as IHasArguments).Arguments.Clear();

            IWithParameters declaration = GetValidDeclaration(originalCall);

            if (declaration == null ||
                !(declaration.Parameters.Count == originalArguments.Count ||
                  (declaration.Parameters.Count == originalArguments.Count + 1 && HasParamArray(declaration)) ||
                  (declaration.Parameters.Count == originalArguments.Count + 1 && IsExtensionMethod(declaration))
                  ))
            {
                return(null);
            }
            int startParamIndex = 0;

            if (IsExtensionMethod(declaration))
            {
                startParamIndex = 1;
            }

            for (int i = 0; i < originalArguments.Count; i++)
            {
                Expression originalArgument = originalArguments[i];
                AttributeVariableInitializer namedArgument = originalArgument as AttributeVariableInitializer;
                if (namedArgument == null)
                {
                    ElementReferenceExpression leftSide = new ElementReferenceExpression(declaration.Parameters[startParamIndex + i].Name);
                    namedArgument           = new AttributeVariableInitializer();
                    namedArgument.LeftSide  = leftSide;
                    namedArgument.RightSide = originalArgument.Clone() as Expression;
                }
                (replacementCall as IHasArguments).Arguments.Add(namedArgument);
            }
            return(CodeRush.CodeMod.GenerateCode(replacementCall, true));
        }
    private static string GetGeneratedCode(LanguageElement originalCall)
		{
      if (!(originalCall is IHasArguments))
				return null;
      ExpressionCollection originalArguments = (originalCall as IHasArguments).Arguments;
			if (originalArguments == null)
				return null;
      LanguageElement replacementCall = originalCall.Clone() as LanguageElement;
      (replacementCall as IHasArguments).Arguments.Clear();
      
      IWithParameters declaration = GetValidDeclaration(originalCall);

      if (declaration == null ||
        !(declaration.Parameters.Count == originalArguments.Count || 
        (declaration.Parameters.Count == originalArguments.Count + 1 && HasParamArray(declaration))
        || (declaration.Parameters.Count == originalArguments.Count + 1 && IsExtensionMethod(declaration))
        ))
				return null;
      int startParamIndex = 0;
      if (IsExtensionMethod(declaration))
        startParamIndex = 1;

			for (int i = 0; i < originalArguments.Count; i++)
			{
        Expression originalArgument = originalArguments[i];
        AttributeVariableInitializer namedArgument = originalArgument as AttributeVariableInitializer;
        if (namedArgument == null)
        {
          ElementReferenceExpression leftSide = new ElementReferenceExpression(declaration.Parameters[startParamIndex + i].Name);
          namedArgument = new AttributeVariableInitializer();
          namedArgument.LeftSide = leftSide;
          namedArgument.RightSide = originalArgument.Clone() as Expression;
        }
        (replacementCall as IHasArguments).Arguments.Add(namedArgument);
			}
			return CodeRush.CodeMod.GenerateCode(replacementCall, true);
		}