It was somewhat easy to do.
Create a console app and add reference to Microsoft.CodeAnalysis.CSharp in your project.
Here is the program that visited all properties in a source text:
using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
class ModelCollector : CSharpSyntaxWalker
{
public Dictionary<string, List<string>> Models { get; } = new Dictionary<string, List<string>>();
public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
var classnode = node.Parent as ClassDeclarationSyntax;
if (!Models.ContainsKey(classnode.Identifier.ValueText))
{
Models.Add(classnode.Identifier.ValueText, new List<string>());
}
Models[classnode.Identifier.ValueText].Add(node.Identifier.ValueText);
}
}
class Program
{
static void Main()
{
var code = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
public class MyAwesomeModel
{
public string MyProperty {get;set;}
public int MyProperty1 {get;set;}
}
}";
var tree = CSharpSyntaxTree.ParseText(code);
var root = (CompilationUnitSyntax)tree.GetRoot();
var modelCollector = new ModelCollector();
modelCollector.Visit(root);
Console.WriteLine(JsonSerializer.Serialize(modelCollector.Models));
}
}