using System;
using System.Collections.Generic;
using System.Text;
namespace PlantUMLClient
{
public interface ITest
{
}
public abstract class AbsTest
{
}
public struct TestStruct
{
}
public enum TestEnum
{
}
class Test:ITest
{
public class Person
{
}
}
public class Student
{
}
}
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.IO;
using System.Linq;
namespace PlantUMLClient
{
class Program
{
static void Main(string[] args)
{
var url = @"Test.cs";
var tree = CSharpSyntaxTree.ParseText(File.ReadAllText(url));
var root = tree.GetRoot();
Console.WriteLine(root.GetType());
var nodes = root.DescendantNodes();
//获取文件中所有的类
var classes = nodes.OfType<ClassDeclarationSyntax>();
var interfaces = nodes.OfType<InterfaceDeclarationSyntax>();
var structs = nodes.OfType<StructDeclarationSyntax>();
var enums = nodes.OfType<EnumDeclarationSyntax>();
foreach (var cls in classes)
{
Console.WriteLine(cls.Identifier);
}
foreach (var item in interfaces)
{
Console.WriteLine(item.Identifier);
}
foreach (var item in structs)
{
Console.WriteLine( item.Identifier);
}
foreach (var item in enums)
{
Console.WriteLine(item.Identifier);
}
}
}
}
网友评论