2

I'm not sure if the title is accurately describing what I'm trying to ask...

Basically, how does Visual Studio look at a code file in a CSPROJ and determine references for a method or variable, or if a using statement is not being used in the code? Obviously it's not just doing simple text parsing, and it seems to work before ever doing a build so I don't think it's referencing the assembly/CLR.

Is there an API that I can tie into from an external app, if I have the user select their CSPROJ or SLN file and be able to report on unused using statements in files, or methods/vars that aren't being called anywhere in their code?

I'm asking because I'm considering building some sort of reporting tool to show areas where code cleanup could occur, but I'm not really sure where to begin.

Thanks!

EDIT: As a followup question, is there a similar tool already out there?

6
  • 1
    I think this is the sort of thing that Microsoft Roslyn is exactly geared for, but I know very little about it. Commented Apr 17, 2013 at 18:33
  • mellamokb Hmm interesting, I'm going to read up on that... Alex I've used resharper and don't recall seeing those features, but I haven't used it too in depth, so I could be wrong. I may need to research that a bit as well. Thanks for the comments! Commented Apr 17, 2013 at 18:35
  • see: Intellisense. There's are also some assemblies with language names that have the parsers in them. Something.Something.CSharp, Something.Something.VisualBasic etc. Commented Apr 17, 2013 at 18:35
  • Looks like you should try ReSharper and than see if you have something to add yourself - there is already plenty of checks that R# does on per-file/project level. Commented Apr 17, 2013 at 18:37
  • 1
    found what I was looking for. I think it's Reed's "black box". Microsoft.CSharp.CSharpCodeProvider still it might have something useful. Commented Apr 17, 2013 at 18:47

1 Answer 1

7

Visual Studio does compilation on the fly, in order to determine many of the things like this, in addition to providing functionality like Intellisense.

Right now, the compilers are all a "black box" and not directly usable. The Roslyn project has the goal of changing this, allowing the full breadth of code analysis being done to be usable. It's currently in a CTP, and could be used now.

EDIT: As a followup question, is there a similar tool already out there?

Many third party extensions, such as Resharper, provide much of this functionality (such as their Safe Delete refactoring and Solution Wide Inspections).

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Reed. This is exactly the information I was looking for, your answer was very helpful. I'm reading up a little bit on Roslyn right now (per your answer and mellamokb's comment).
One more follow up question. In your opinion, do you think Resharper is using Roslyn for it's code analysis features?
n/m Roselyn seems quite new and I know Resharper has been around for a long time.
@AdamPlocher I suspect JetBrains has their own "C# compiler" they use for their analysis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.