public static async Task <string> CreateComplexTypeClass(MethodDeclarationSyntax wcfMethod, string methodName, bool isActiveWcfMethod) { if (!isActiveWcfMethod) { return(""); } var parameters = wcfMethod.ParameterList.Parameters; // add return type as class property if has 'out' keyword if (!OutKeywordGenerator.AnyHaveOutKeyword(parameters)) { return(""); } // create complex class var className = ComplexTypeNamesMapper.MapToClientComplexClassName(methodName); var complexClassDeclaration = await MultipleComplexTypesGenerator.CreateClassFromComplexTypes(parameters, className); // create return type property var wcfReturnType = wcfMethod.ReturnType.NormalizeWhitespace().ToFullString(); var returnTypePropertyCode = $"public {wcfReturnType} Result {{ get; set;}}"; var returnTypeProperty = (await CodeParser.ConvertToMemberDeclarations(returnTypePropertyCode)).Single(); // add return type property to complex class var members = complexClassDeclaration.Members.Add(returnTypeProperty); complexClassDeclaration = complexClassDeclaration.WithMembers(members); var complexTypeClass = complexClassDeclaration?.NormalizeWhitespace().ToFullString(); return(complexTypeClass); }
private static List <SyntaxNodeOrToken> CreateWcfArguments(SeparatedSyntaxList <ParameterSyntax> wcfParameters) { var arguments = wcfParameters // add arguments using wcf method parameters .Select(parameter => { var argument = SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameter.Identifier)); // add 'out' keyword if param has it var hasOutKeyword = OutKeywordGenerator.HasOutKeyword(parameter); argument = hasOutKeyword ? OutKeywordGenerator.AddOutKeywordToArgument(argument) : argument; return(argument); }) // add commas .SelectMany(param => new SyntaxNodeOrToken[] { param, SyntaxFactory.Token(SyntaxKind.CommaToken) }) // remove last trailing comma .Take(wcfParameters.Count * 2 - 1) .ToList(); return(arguments); }
public static BlockSyntax TransformBlockWithOutKeyword(BlockSyntax block, SeparatedSyntaxList <ParameterSyntax> wcfParameters, IEnumerable <SyntaxNodeOrToken> arguments, string methodName) { if (!OutKeywordGenerator.AnyHaveOutKeyword(wcfParameters)) { return(block); } block = AddStatementsForOutKeyword(block, arguments, wcfParameters, methodName); block = TransformReturnStatement(block, methodName); return(block); }
public static string TransformOutKeywordReturnType(string wcfReturnType, SeparatedSyntaxList <ParameterSyntax> wcfParameters, string wcfMethodName) { var isVoidReturnType = wcfReturnType == "void"; if (!OutKeywordGenerator.AnyHaveOutKeyword(wcfParameters) || isVoidReturnType) { return(wcfReturnType); } var complexVariableName = ComplexTypeNamesMapper.MapToServiceComplexClassType(wcfMethodName); var returnType = complexVariableName; return(returnType); }
private static List <string> GetArgumentNames(IEnumerable <SyntaxNodeOrToken> arguments) { var names = arguments .Where(argument => { var hasOutKeyword = OutKeywordGenerator.HasOutKeywordForArgument(argument); return(hasOutKeyword); }) .Select(argument => { var argumentName = OutKeywordGenerator.RemoveOutKeyword(argument.ToFullString()); return(argumentName); }) .ToList(); return(names); }
private static List <string> GetParameterNamesWithOutKeywords(IEnumerable <ParameterSyntax> parameters) { var names = parameters .Where(parameter => { var hasOutKeyword = OutKeywordGenerator.HasOutKeyword(parameter); return(hasOutKeyword); }) .Select(parameter => { var parameterName = parameter.Identifier.ValueText; return(parameterName); }) .ToList(); return(names); }
public static string TransformMethodBlockWithOutKeywords(string block, MethodDeclarationSyntax wcfMethod, bool isActiveWcfMethod) { if (!isActiveWcfMethod) { return(block); } var wcfParameters = wcfMethod .ParameterList .Parameters; var hasOutKeyword = OutKeywordGenerator.AnyHaveOutKeyword(wcfParameters); if (!hasOutKeyword) { return(block); } var blockDeclaration = (BlockSyntax)SyntaxFactory.ParseStatement($"{block}"); var parameterNames = GetParameterNamesWithOutKeywords(wcfParameters); // add to start of method block var firstStatementIndex = 0; var startStatements = CreateStartStatements(parameterNames, wcfParameters); var statements = blockDeclaration.Statements.InsertRange(firstStatementIndex, startStatements); // add before end (i.e. before the return statement) of method block var oldStatement = FindStatementToReplace(statements); var endStatements = CreateEndStatements(parameterNames, wcfParameters, wcfMethod); statements = statements.ReplaceRange(oldStatement, endStatements); // finialize block blockDeclaration = blockDeclaration.WithStatements(statements); block = blockDeclaration.ToFullString(); return(block); }
public static ClassDeclarationSyntax TransformComplexClassWithOutKeyword(ClassDeclarationSyntax complexClass, SeparatedSyntaxList <ParameterSyntax> wcfParameters, string wcfReturnType) { if (!OutKeywordGenerator.AnyHaveOutKeyword(wcfParameters)) { return(complexClass); } var classProperties = complexClass.Members.OfType <PropertyDeclarationSyntax>(); var outKeywordProperty = $"public {wcfReturnType} Result {{ get; set;}}"; var outKeywordPropertyDeclaration = SyntaxFactory.ParseCompilationUnit(outKeywordProperty) .DescendantNodes() .OfType <PropertyDeclarationSyntax>() .First(); var newProperties = new List <PropertyDeclarationSyntax>(); newProperties.AddRange(classProperties); newProperties.Add(outKeywordPropertyDeclaration); var newComplexClass = complexClass.WithMembers(SyntaxFactory.List <MemberDeclarationSyntax>(newProperties)); return(newComplexClass); }