简体   繁体   English

以编程方式检查.NET代码

[英]Programmatically inspect .NET code

I am looking for a way to programmatically inspect a .NET (C#, VB.NET,..) source code to perform static code analysis. 我正在寻找一种方法来以编程方式检查.NET(C#,VB.NET,..)源代码以执行静态代码分析。

I'd like to perform queries on the code such as: - list classes whose name begin by x - list all the subclasses of x - list methods which instanciate an object of class x - determine if method x contains a variable named y - list methods calling the method y - ... 我想对代码执行查询,例如: - 名称以x开头的列表 - 列出x的所有子类 - 列出类x的对象的列表方法 - 确定方法x是否包含名为y的变量 - 列表调用方法y的方法 - ...

What I am looking for is an API or something else allowing me to write programs able to examine a source code. 我正在寻找的是一个API或其他东西,允许我编写能够检查源代码的程序。

NDepend提供了一种类似SQL的查询语言,用于查询.NET代码结构。

You can use System.Reflection, that should do the trick nicely for some of the things you want. 你可以使用System.Reflection,它可以很好地完成你想要的一些事情。 As far as getting into the IL itself, check out Mono's Cecil . 至于进入IL本身,请查看Mono的Cecil

为什么不使用FxCop进行静态代码分析?

See the DMS Software Reengineering Toolkit . 请参阅DMS Software Reengineering Toolkit

DMS provides parsers that automatically build ASTs for many languages (C, C++, Java, C# [1.2, 2.0, 3.0 and 4.0], COBOL, ECMAScript, PHP, Verilog, ..) as well as symbol tables and control and data flow analysis for several of these. DMS提供自动构建多种语言(C,C ++,Java,C#[1.2,2.0,3.0和4.0],COBOL,ECMAScript,PHP,Verilog ......)的AST的解析器,以及符号表和控制及数据流分析对于其中的几个。

DMS's pattern language can be used to match surface-syntax patterns, and combined with procedural analysis to ties code elements together with symbol table entries and various data flow relations. DMS的模式语言可用于匹配表面语法模式,并与过程分析相结合,将代码元素与符号表条目和各种数据流关系联系起来。 It has been used to implement a wide variety of program analysis tools, and is designed to be a foundation for you to build you own tool, without wasting a vast amount of time building basic program analysis infrastructure. 它已被用于实现各种程序分析工具,旨在为您构建自己的工具奠定基础,而不会浪费大量时间来构建基本的程序分析基础结构。

我建议使用Roslyn

What about using the code model in Reflector? 在Reflector中使用代码模型怎么样? With the code model view add-in you should be able to get the idea of how to interrogate the structure of the code. 使用代码模型视图加载项,您应该能够了解如何查询代码的结构。

What about StyleCop? StyleCop怎么样? http://code.msdn.microsoft.com/sourceanalysis . http://code.msdn.microsoft.com/sourceanalysis But it doesn't support APIs. 但它不支持API。

To complete the stusmith's answer, NDepend seems to be what you are looking for. 为了完成stusmith的回答,NDepend似乎就是你要找的东西。 NDepend lets write Code Queries and Rules over LINQ Queries , (what we call CQLinq ). NDepend允许通过LINQ查询编写代码查询和规则 (我们称之为CQLinq )。 Disclaimer: I am one of the developers of the tool 免责声明:我是该工具的开发人员之一

For example here are some CQLinq queries: 例如,这里有一些CQLinq查询:

-> list classes whose name begin by x - >列出名称以x开头的类

from t in Application.Types.WithNameLike("^x")
where t.IsClass select t

-> list all the subclasses of x - >列出x的所有子类

from t in Application.Types
where t.DeriveFrom("MyNamespace.MyTypeX")
select t

-> list methods which instanciate an object of class x - >列出实现类x对象的方法

from m in Application.Methods
where m.CreateA("MyNamespace.MyTypeX")
select m

-> list methods calling the method y - ... - >调用方法y的列表方法 - ...

from m in Application.Methods
where m.IsUsing("MyNamespace.MyType.MyMethodY()")
select m

More than 200 code rules are proposed by default. 默认情况下,提出了200多个代码规则 Customizing existing rules or creating your own rules is straightforward thanks to the well-known C# LINQ syntax. 由于众所周知的 C#LINQ语法,自定义现有规则或创建自己的规则非常简单。

CQLinq queries can be edited live in VisualStudio, and offer instant result, with browsing facilities: CQLinq查询可以在VisualStudio中实时编辑,并提供即时结果,具有浏览功能:

在此输入图像描述

Actually, CQLinq is based on NDepend.API , and more specifically on types in the namespace NDepend.CodeModel . 实际上,CQLinq基于NDepend.API ,更具体地说是基于命名空间NDepend.CodeModel中的类型。 With NDepend.API you can write programs to do things more tricky tghan just CQLinq queries, for example we wrote a Code Duplicate Finder tool with NDepend.API. 使用NDepend.API,您可以编写程序来处理更加棘手的问题,例如CQLinq查询,例如我们使用NDepend.API编写了一个Code Duplicate Finder工具

Rules can be verified live in Visual Studio and at Build Process time, in a generated HTML+javascript report . 可以在生成的HTML + javascript报告中 在Visual Studio和Build Process时间实时验证规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM