I am looking for a way to programmatically inspect a .NET (C#, VB.NET,..) source code to perform static code analysis.
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 - ...
What I am looking for is an API or something else开发者_运维知识库 allowing me to write programs able to examine a source code.
NDepend gives a SQL-like query language for querying .NET code structure.
You can use System.Reflection, that should do the trick nicely for some of the things you want. As far as getting into the IL itself, check out Mono's Cecil.
Why not use FxCop for static code analysis?
See the 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'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. 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.
What about using the code model in 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? http://code.msdn.microsoft.com/sourceanalysis. But it doesn't support APIs.
To complete the stusmith's answer, NDepend seems to be what you are looking for. NDepend lets write Code Queries and Rules over LINQ Queries, (what we call CQLinq). Disclaimer: I am one of the developers of the tool
For example here are some CQLinq queries:
-> list classes whose name begin by x
from t in Application.Types.WithNameLike("^x")
where t.IsClass select t
-> list all the subclasses of x
from t in Application.Types
where t.DeriveFrom("MyNamespace.MyTypeX")
select t
-> list methods which instanciate an object of class x
from m in Application.Methods
where m.CreateA("MyNamespace.MyTypeX")
select m
-> list methods calling the method y - ...
from m in Application.Methods
where m.IsUsing("MyNamespace.MyType.MyMethodY()")
select m
More than 200 code rules are proposed by default. Customizing existing rules or creating your own rules is straightforward thanks to the well-known C# LINQ syntax.
CQLinq queries can be edited live in VisualStudio, and offer instant result, with browsing facilities:
Actually, CQLinq is based on NDepend.API, and more specifically on types in the namespace 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.
Rules can be verified live in Visual Studio and at Build Process time, in a generated HTML+javascript report.
I would recommend using Roslyn for this.
精彩评论