0

I am creating a Visual Studio plugin to analyze code. I am using EnvDTE.ProjectItem to access project and other project items.

I want to list properties and methods inside a class. Is there any way that I can access code inside a class?

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47

1 Answers1

0

There are two possible ways for that:

  • First you can use reflection on the compiled assembly

or

  • You can try Roslyn, which is a code analyzer (for C#, VB .Net). This is good for not only getting properties of code, but also can come in handy to restructure the existing, and creating new ones. A good intruductiory video on Channel 9
Ursegor
  • 878
  • 8
  • 16
  • Thanks for the reply. I didn't get your first option?? Can i use roslyn in vs add-in – Hasitha De Silva Mar 31 '14 at 07:26
  • Welcome! If you have the assembly of your targeted code, you can search for information on available classes, methods, etc. with reflection (but you can't get implementation information of method bodies). Second, on this [forum](http://social.msdn.microsoft.com/Forums/vstudio/en-US/4bc89531-bc13-4763-8a76-3132c2160a28/using-roslyn-to-parse-source-code-from-an-addin-to-vs?forum=roslyn) the Add-In theme is discussed. I think you can use Roslyn also from an Add-In, but it has a strict terms of use. AFAIK it is prohibited to post any code samples. I think you can use it, but with restrictions. – Ursegor Apr 01 '14 at 18:12
  • Any andvaces with it? – Ursegor Apr 03 '14 at 15:24
  • Hi Richard i was able to access namespace, class and method.But i couldn't access method bodies.I didn't use Roslyn what i did was i loop through namespace and then classes then i was able to access the code inside the class. – Hasitha De Silva Apr 04 '14 at 07:43
  • That sounds good, but does not seem to help you analysing the code as you pointed as a requirement in your question (you are going to do the hard work, but it depends on how difficult it is of course). Roslyn could come in handy because you can create a syntax tree, etc. with little effort, but anyway good luck! A recent news I have heard of that Roslyn and it's code are free to use and available on [codeplex](http://roslyn.codeplex.com/). – Ursegor Apr 04 '14 at 14:56