Roslyn 静态分析
本文告诉大家如何使用 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 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。
Roslyn 静态分析的更多相关文章
- Roslyn 入门:使用 Roslyn 静态分析现有项目中的代码
Roslyn 是微软为 C# 设计的一套分析器,它具有很强的扩展性.以至于我们只需要编写很少量的代码便能够分析我们的项目文件. 作为 Roslyn 入门篇文章,你将可以通过本文学习如何开始编写一个 R ...
- 在 Roslyn 分析语法树时添加条件编译符号的支持
我们在代码中会写 #if DEBUG 或者 [Conditional("DEBUG")] 来使用已经定义好的条件编译符号.而定义条件编译符号可以在代码中使用 #define WAL ...
- Roslyn 入门:使用 .NET Core 版本的 Roslyn 编译并执行跨平台的静态的源码
Roslyn 是微软为 C# 设计的一套分析器,它具有很强的扩展性.以至于我们只需要编写很少量的代码便能够编译并执行我们的代码. 作为 Roslyn 入门篇文章之一,你将可以通过本文学习如何开始编写一 ...
- Roslyn 入门:使用 Visual Studio 的语法可视化窗格查看和了解代码的语法树
使用 Visual Studio 提供的 Syntax Visualizer,我们可以实时看到一个代码文件中的语法树.这对我们基于 Roslyn 编写静态分析和修改工具非常有帮助.本文将介绍如何安装它 ...
- dotnet 从入门到放弃的 500 篇文章合集
本文是记录我从入门到放弃写的博客 博客包括 C#.WPF.UWP.dotnet core .git 和 VisualStudio 和一些算法,所有博客使用 docx 保存 下载:dotnet 从入门到 ...
- 【转】C# 的 IDisposable 接口
C# 的 IDisposable 接口 我在微软的团队快被微软 C# 里面的各种 IDisposable 对象给折腾疯了…… 故事比较长,先来科普一下.如果你没有用过 C#,IDisposable 是 ...
- WPF 程序无法触摸操作?我们一起来找原因和解决方法!
WPF 自诞生以来就带着微软先生的傲慢.微软说 WPF 支持触摸,于是 WPF 就真的支持触摸了.对,我说的是"支持触摸",那种摸上去能点能动的:偶尔还能带点儿多指的炫酷效果.但是 ...
- 2018-8-29-Roslyn-静态分析
title author date CreateTime categories Roslyn 静态分析 lindexi 2018-08-29 09:10:19 +0800 2018-03-13 14: ...
- 2018-8-10-dotnet-从入门到放弃的-500-篇文章合集
title author date CreateTime categories dotnet 从入门到放弃的 500 篇文章合集 lindexi 2018-08-10 19:16:52 +0800 2 ...
随机推荐
- 拦截导弹 (最长上升子序列LIS)
#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; ] ...
- Location 位置 history
拆分出来地址 让地址各归其位 search案例 查找历史记录跳转
- 2018-5-22-SublimeText-粘贴图片保存到本地
title author date CreateTime categories SublimeText 粘贴图片保存到本地 lindexi 2018-05-22 15:15:26 +0800 2018 ...
- php中array_slice和array_splice函数解析方式方法
array_slice array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_k ...
- 字符串Hash算法比较
基本概念所谓完美哈希函数,就是指没有冲突的哈希函数,即对任意的 key1 != key2 有h(key1) != h(key2).设定义域为X,值域为Y, n=|X|,m=|Y|,那么肯定有m> ...
- 在Swift中检查API的可用性
http://www.cocoachina.com/swift/20150901/13283.html 本文由CocoaChina译者ALEX吴浩文翻译自Use Your Loaf博客 原文:Chec ...
- python 自主控制异常:用户自定义异常
- find 使用搜集
find:-atime +n/-n:表示访问或执行时间大于或小于n天的文件-ctime +n/-n:表示写入.更改inode属性的时间大于或小于n天的文件-mtime +n/-n:表示写入时间大于或小 ...
- 51nod 1686 第K大区间【离散化+二分】
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 定义一个区间的值为其众数出现的次数. 现给出n ...
- Hbase数据模型物理视图