Roslyn C#源码分析

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();
            var interfaces = nodes.OfType();
            var structs = nodes.OfType();
            var enums = nodes.OfType();
            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);
            }
        }
    }

}

你可能感兴趣的:(Roslyn C#源码分析)