public static IReadOnlyCollection <MyType> Parse(string[] parentFieldNames, AnonymousObjectCreationExpressionSyntax anonymousObjectCreationExpressionSyntax) { var allTypes = new List <MyType>(); var properties = new List <MyProperty>(); foreach (var initializer in anonymousObjectCreationExpressionSyntax.Initializers) { var name = initializer.NameEquals.Name.Identifier.Text; if (initializer.Expression is AnonymousObjectCreationExpressionSyntax subDir) { var subDirTypes = AnonymousObjectCreationParser.Parse(parentFieldNames.Concat(new[] { name }).ToArray(), subDir); allTypes.AddRange(subDirTypes); var classObj = subDirTypes.Last() ?? throw new InvalidOperationException(); var className = classObj.Name; var props = string.Join(",", classObj.Properties.Select(_ => _.Expression)); var property = new MyProperty( name, className, $"new {className}({props})"); properties.Add(property); } else if (initializer.Expression is InvocationExpressionSyntax invocationExpression) { var methodName = invocationExpression.Expression.ToString(); if (methodName.EndsWith("Files.FolderFunctions")) { var property = new MyProperty(name, "FolderFunctions", string.Join(".", parentFieldNames) + $".{name}.Clone()"); properties.Add(property); } else { var property = new MyProperty(name, "IPhysicalFile", string.Join(".", parentFieldNames) + $".{name}.Clone()"); properties.Add(property); } } else { var property = new MyProperty(name, "IPhysicalFile", string.Join(".", parentFieldNames) + $".{name}.Clone()"); properties.Add(property); } } var newClassName = parentFieldNames.Last() + "Type"; var counter = 2; while (allTypes.Any(_ => _.Name == newClassName)) { newClassName = parentFieldNames.Last() + $"Type{counter}"; counter++; } allTypes.Add(new MyType(newClassName, properties)); return(allTypes); }
private static MyRootType Parse(FieldDeclarationSyntax fieldDeclarationSyntax) { var list = fieldDeclarationSyntax.Declaration.Variables.ToList(); if (list.Count == 0) { return(null); } var variableDeclarationSyntax = list.First(); if (variableDeclarationSyntax.Initializer == null) { return(null); } var fieldName = variableDeclarationSyntax.Identifier.Text; var classDeclarationSyntax = GetClassDeclarationSyntax(fieldDeclarationSyntax); var className = classDeclarationSyntax.Identifier.Text; var namespaceName = GetNamespaceDeclarationSyntax(fieldDeclarationSyntax); var fieldValueSyntax = variableDeclarationSyntax.Initializer.Value; if (fieldValueSyntax is AnonymousObjectCreationExpressionSyntax anonymousObjectCreationExpressionSyntax) { var types = AnonymousObjectCreationParser.Parse( new[] { fieldName }, anonymousObjectCreationExpressionSyntax); var rootType = new MyRootType( namespaceName, className, types.Last().Properties, types.Take(types.Count - 1).ToArray()); return(rootType); } return(null); }