zoukankan      html  css  js  c++  java
  • Roslyn词法分析器初使用

    需:install-package:Microsoft.CodeAnalysis 

    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.CSharp.Syntax;
    using Microsoft.CodeAnalysis.VisualBasic.Syntax;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;


    namespace RoslynTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var text = @"
    namespace RoslynTest
    {
            public interface ICalculator
            {
               
                public static int Evaluate(string a,string b);
                public static string Evaluate2(string a1,string b1);
            }
    }";
                string path = @"E:ND.ApplicationND.Lib.ApplicationNDLibND.WebServiceND.WebService.ContractNDFront.WebService.ContracthotelIHotelFacility.cs";
                text = File.ReadAllText(path);
            SyntaxTree tree=  Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(text);
            var root = (Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax)tree.GetRoot();
            var firstMember = root.Members[0];
            NamespaceDeclarationSyntax NameSpaceDeclaration = (NamespaceDeclarationSyntax)firstMember;
            InterfaceDeclarationSyntax interfaceDeclaration = (InterfaceDeclarationSyntax)NameSpaceDeclaration.Members[0];
            Console.WriteLine("命名空间:" + NameSpaceDeclaration.Name);
            Console.WriteLine("接口名:" + interfaceDeclaration.Identifier.Value);

               
            IEnumerable<SyntaxAnnotation> ss = interfaceDeclaration.GetAnnotations("ICalculator");
           ss.ToList().ForEach(x =>
           {
               Console.WriteLine("接口描述:" + x.Data); 
           });
          
            interfaceDeclaration.Members.ToList().ForEach(y =>
            {
                
                MethodDeclarationSyntax methodDeclaration = (MethodDeclarationSyntax)y;
                var paramsDeclaration = methodDeclaration.ParameterList.Parameters;
                
                Console.WriteLine("返回类型:" + methodDeclaration.ReturnType + "方法名称:" + methodDeclaration.Identifier);
                paramsDeclaration.ToList().ForEach(x =>
                {
                    
                    Console.WriteLine("参数类型:" + x.Type + ",参数名称:" + x.Identifier);
                });
            });
                
         
           
              Console.ReadKey();
            }

          
        }
    }

  • 相关阅读:
    04.VUE学习之v-text v-html
    03.VUE学习之动态绑定值
    2019年Vue学习路线图
    02.VUE学习二之数据绑定
    01.VUE学习一
    一张图解析FastAdmin中的表格列表的功能
    python 正则表达式与JSON字符串
    jQuery的select2下拉框的搜索功能(使用select2插件,方便简单)
    笔记1 python入门学习笔记
    MySQL----Navicat使用
  • 原文地址:https://www.cnblogs.com/taomylife/p/4727367.html
Copyright © 2011-2022 走看看