Search code examples
c#visual-studiovisual-studio-2017visual-studio-extensions

Is it possible to let visual studio extension access the objects of opened solution?


I want to make an extension, which will iterate through WPF or Windows Forms Controls (or any object) of actually opened project. Is it possible for extension to access/get the objects of that solution/project?


Solution

  • Is it possible for extension to access/get the objects of that solution/project?

    Yes! You have a few choices.

    1. Managed Package Framework

    The name is misleading but this originally Microsoft Codeplex project (now available on NuGet) allows you to create something as simple as a:

    • menu items; toolbar buttons; windows and panes

    ...or something mind-blowingly sophisticated as:

    • something akin to ReSharper or
    • even creating your own language

    Documentation is terrible. Your best bet is to look at the Microsoft source code for the Python MPF example.

    MPF will let you iterate everything the VS solution in a nice object hierarchy in realtime as you type or stare at your code.

    2. T4 Templates

    You don't even need to leave your current code. T4 templates inside a VS project can interrogate items in your solution at compile time only.

    T4 templates resemble Razor syntax in the way code snippets is interleaved with template content. In fact T4 is used in ASP.NET MVC Razor without peeps knowing. You could be up and running with T4 syntax way before MPF due to the latter's steep learning curve.

    The unfortunate thing though is that T4 is geared up to perform code generation so that's kinda a downer if all you want to do is enumerate items.