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 静态分析相关推荐
- 课程 预编译框架,开发高性能应用 - 微软技术暨生态大会 2018
微软技术暨生态大会(Tech Summit),2018 年在上海世博中心召开.这是最后一次的 Tech Summit 了:明年开始,中国大陆地区就要和其他国家和地区一样,进行全球 Ignite Tou ...
- 2018-8-10-dotnet-从入门到放弃的-500-篇文章合集
title author date CreateTime categories dotnet 从入门到放弃的 500 篇文章合集 lindexi 2018-08-10 19:16:52 +0800 2 ...
- dotnet 从入门到放弃的 500 篇文章合集
本文是记录我从入门到放弃写的博客 博客包括 C#.WPF.UWP.dotnet core .git 和 VisualStudio 和一些算法,所有博客使用 docx 保存 下载:dotnet 从入门到 ...
- Linux平台下QtCreator集成代码静态分析工具clang-tidy和Clazy
Linux平台下QtCreator集成代码静态分析工具clang-tidy和Clazy 原文连接: https://blog.csdn.net/wsj18808050/article/details/ ...
- 静态分析C语言生成函数调用关系的利器——cflow
除了<静态分析C语言生成函数调用关系的利器--calltree>一文中介绍的calltree,我们还可以借助cflow辅助我们阅读理解代码.(转载请指明出于breaksoftware的cs ...
- 使用roslyn代替MSBuild完成解决方案编译
原本我是使用批处理调用 MSBuild 完成解决方案编译的,新版的 MSBuild 在 Visual Studio 2015 会自带安装. 当然在Visual Studio 2015 中,MSBuil ...
- Facebook新推出AL语言,意在简化程序静态分析
AL是一种易用的声明式编程语言,适用于抽象语法树(AST)推理,使开发人员可以扩展Facebook Infer静态分析器的功能. Infer采用OCaml编写,可标识Null指针访问.资源和内存泄漏, ...
- VS2017 Pro未能找到路径“……\bin\roslyn\csc.exe”的解决方案
VS2017 Pro未能找到路径"--\bin\roslyn\csc.exe"的解决方案 参考文章: (1)VS2017 Pro未能找到路径"--\bin\roslyn\ ...
- roslyn生成html,Visual Studio 2017 bin \ roslyn文件在生成期间被锁定
Visual Studio 2017 bin \ roslyn文件在生成期间被锁定 我正在运行VS2017版本26430.13,每次尝试构建Web项目时,都会收到错误,拒绝访问bin \ roslyn ...
- android黑科技系列——静态分析技术来破解Apk
一.前言 从这篇文章开始我们开始我们的破解之路,之前的几篇文章中我们是如何讲解怎么加固我们的Apk,防止被别人破解,那么现在我们要开始破解我们的Apk,针对于之前的加密方式采用相对应的破解技术,And ...
最新文章
- error: RPC failed; curl 18 transfer closed with outstanding read
- android fastjson漏洞_【漏洞预警】Fastjson 远程代码执行漏洞(暂无PoC)
- css箭头超链接,css超链接
- 判断两个矩形相交以及求出相交的区域
- 一种情感判别分析体系在汽车品牌舆情管理中的应用
- python有几大模块_Python的几个常用模块
- 附件计算器中的MC、MR、MS、M+作用
- Behavior Targeting - 技术研究
- 贺利坚老师汇编课程24笔记:内存单元[...]和(...)内容
- 机器学习重塑供应链管理的10个途径
- 【车辆计数】基于matlab形态学停车场车辆计数【含Matlab源码 628期】
- Nagios 监控温度感应器
- 05 - 雷达的发展与应用
- c语言无法定位程序输入点,WinXP提示无法定位程序输入点于动态链接库上的快速解决办法...
- 自己动手搭建聊天APP
- 爱普生发票打印机 每次连续打印发票,都跳页,中间一张空白
- 让你的Win10桌面比macOS还简洁漂亮高大上
- 智能车八邻域图像算法_二
- angular ng lint 相关
- 极路由4/b70刷华硕(padavan)教程