Thanks: Dave Glick - Announcing Scripty https://daveaglick.com/posts/announcing-scripty
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}");
}
}