本文告诉大家如何使用 Roslyn 分析代码

首先创建一个项目,项目使用.net Framework 4.6.2 ,控制台项目。然后需要安装一些需要的库

Nuget 安装

打开 Nuget 安装下面两个库

  • Microsoft.CodeAnalysis.CSharp

  • Microsoft.CodeAnalysis.CSharp.Workspaces

  • Newtonsoft.Json

使用

下面来写简单的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TrrluujHlcdyqa
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hellow");
}
} class Foo
{
public string KiqHns { get; set; }
}
}

对上面的代码分析

首先需要把上面的代码放在字符串

然后创建分析代码,读取代码。

   class ModelCollector : CSharpSyntaxWalker
{
public readonly Dictionary<string, List<string>> Models = new Dictionary<string, List<string>>();
public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
var classnode = node.Parent as ClassDeclarationSyntax;
if (classnode != null && !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(string[] args)
{
string str = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TrrluujHlcdyqa
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(""hellow"");
}
} class Foo
{
public string KiqHns { get; set; }
}
}"; var tree = CSharpSyntaxTree.ParseText(str); var root = (CompilationUnitSyntax)tree.GetRoot();
var modelCollector = new ModelCollector();
modelCollector.Visit(root);
Console.WriteLine(JsonConvert.SerializeObject(modelCollector.Models)); }
}

这时输出{"Foo":["KiqHns"]}

上面的代码从 https://stackoverflow.com/a/22881532/6116637 学的

更多关于 Roslyn 请看 手把手教你写 Roslyn 修改编译

参见:

通过Roslyn构建自己的C#脚本(更新版) - 天方 - 博客园

Getting Started with Roslyn Analyzers

代码分析 - 借助与 NuGet 集成的 Roslyn 代码分析来生成和部署库

roslyn-analyzers/ReadMe.md at master · dotnet/roslyn-analyzers

In-memory C# compilation and .dll generation using Roslyn

我搭建了自己的博客 https://lindexi.gitee.io/ 欢迎大家访问,里面有很多新的博客。只有在我看到博客写成熟之后才会放在csdn或博客园,但是一旦发布了就不再更新

如果在博客看到有任何不懂的,欢迎交流,我搭建了 dotnet 职业技术学院 欢迎大家加入


本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

最新文章

  1. iOS面试题总结 (二)
  2. go语言文件操作,这期资料比较详细( 欢迎加入go语言群: 218160862 )
  3. linux原始套接字(4)-构造IP_UDP
  4. hdu 1029 Ignatius ans the Princess IV
  5. Java中的super与this解析
  6. poj3252 Round Numbers
  7. Recover a file even if it was not committed but it has to have been added when you use git reset head by mistake.
  8. CodeForces 52C Circular RMQ(间隔周期段树,间隔更新,间隔总和)
  9. JavaScript 构造函数 prototype属性和_proto_和原型链 constructor属性 apply(),call()和bind() 关键字this
  10. eclipse导出jar(含依赖)三步走
  11. parted分区及挂载实战操作大全
  12. Spring cloud zuul跨域(一)
  13. 饮冰三年-人工智能-Python-26 Django 学生管理系统
  14. 23 创建ArcMap启动日志
  15. P2146 [NOI2015]软件包管理器
  16. ionic cordova plugin 安装和使用
  17. python -- 约束、异常处理、MD5
  18. OSLab多线程
  19. Hadoop-调优剖析
  20. C++11 constexpr使用

热门文章

  1. iOS开发——你真的会用SDWebImage?
  2. Leetcode929.Unique Email Addresses独特的电子邮件地址
  3. PHP-xdebug+PHPStorm的debug安装(未完)
  4. Sum Root to Leaf Numbers深度优先计算路径和
  5. Qt qmake报错(TypeError: Property &#39;asciify&#39; of object Core::Internal::UtilsJsExtension)
  6. 集合案例--对ArrayList容器中的内容进行排序
  7. zabbix程序架构
  8. 微服务开源生态报告 No.2
  9. Gym-101623H_High Score
  10. Linux下安装MySQL-python