Created
October 28, 2017 03:20
-
-
Save hhyyg-test/d3ec7f6d7463f85e942896d88f8da741 to your computer and use it in GitHub Desktop.
Script for Controller.cs / Thanks: Dave Glick - Announcing Scripty https://daveaglick.com/posts/announcing-scripty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.CodeAnalysis; | |
| using System.Web; | |
| using System.Linq; | |
| using System.Collections.Generic; | |
| using System.Reflection; | |
| using Microsoft.CodeAnalysis; | |
| using Microsoft.CodeAnalysis.CSharp; | |
| using Microsoft.CodeAnalysis.CSharp.Syntax; | |
| using Microsoft.CodeAnalysis.CSharp.Extensions; | |
| //Document : http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/Workspace/Solution/Document.cs | |
| //c# - Order constructor parameters with Roslyn - Stack Overflow https://stackoverflow.com/questions/30986360/order-constructor-parameters-with-roslyn | |
| public class ClassInfo | |
| { | |
| public string Name { get; set; } | |
| public string[] ConstructorParameters { get; set; } | |
| } | |
| var controllerList = new List<ClassInfo>(); | |
| foreach (Document document in Project.Analysis.Documents) | |
| { | |
| if (document.FilePath.EndsWith("Controller.cs")) | |
| { | |
| SyntaxTree tree = document.GetSyntaxTreeAsync().Result; | |
| var c = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().First(); | |
| string controllerClassName = c.Identifier.ToString(); | |
| var constructor = c.ChildNodes().OfType<ConstructorDeclarationSyntax>().First(); | |
| var parameters = constructor.ParameterList | |
| .ChildNodes() | |
| .Cast<ParameterSyntax>() | |
| .OrderBy(node => ((IdentifierNameSyntax)node.Type).Identifier.ToString()) | |
| .Select(node => SyntaxFactory.Parameter( | |
| SyntaxFactory.List<AttributeListSyntax>(), | |
| SyntaxFactory.TokenList(), | |
| SyntaxFactory.ParseTypeName(((IdentifierNameSyntax)node.Type).Identifier.Text), | |
| SyntaxFactory.Identifier(node.Identifier.Text), | |
| null)); | |
| var controllerInfo = new ClassInfo(); | |
| controllerInfo.Name = controllerClassName; | |
| controllerInfo.ConstructorParameters = parameters.SelectMany(p => p.ChildNodes().Select(child => child.ToString())).ToArray(); | |
| controllerList.Add(controllerInfo); | |
| } | |
| } | |
| foreach(var info in controllerList) | |
| { | |
| Output.WriteLine($"//Controller Class Name: {info.Name} "); | |
| foreach(var parameter in info.ConstructorParameters) | |
| { | |
| Output.WriteLine($"//Parameter Type Name: {parameter}"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment