Visual Studio extensibility (add-in): How to access Silverlight-related properties? - visual-studio

This is the question that has been posted to MSDN forums some time ago, and stayed unanswered to this day:
http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/676b13d4-acfc-4252-b102-5fc0553e4b81/
The property I'm interested in is ProjOutputReferences, stored in the Visual Studio solution (.sln) file.
In Visual Studio, this property is accessible through Property Pages dialog of a Silverlight WebSite project (requires that you have Silverlight Tools for VS2008 installed). There, there is a page called "Silverlight Applications" on which the content of the above mentioned property can be edited.
I need to access it programmatically inside my add-in, through VS automation or low-level interface(s).

In the latest released version of the Silverlight Tools for VS 2008 SP1, the list is persisted in the SilverlightApplicationList property in the referring project file.
For example, I have SilverlightApplication2 and SilverlightApplication2.Web in my solution (the latter references the former). I have the following node in my SilverlightApplication2.Web.csproj file:
<SilverlightApplicationList>{BBA7B148-42AE-477E-BB5E-0BA5AEC0A467}|..\SilverlightApplication2\SilverlightApplication2.csproj|ClientBin|False</SilverlightApplicationList>
There really isn’t a way to access this property via purely DTE, but you can use the Visual Studio SDK / VSIP interfaces to do so (specifically, you want to get the IVsBuildPropertyStorage interface for access to MSBuild properties). Here is a code snippet (runs in a menu command handler in a VSPackage):
IVsSolution solution = GetService(typeof(SVsSolution)) as IVsSolution;
IVsHierarchy hierarchy;
solution.GetProjectOfUniqueName(#"SilverlightApplication2.Web\SilverlightApplication2.Web.csproj", out hierarchy);
IVsBuildPropertyStorage buildPropertyStorage = hierarchy as IVsBuildPropertyStorage;
if (buildPropertyStorage != null)
{
string silverlightAppListValue;
buildPropertyStorage.GetPropertyValue("SilverlightApplicationList", "Debug", (uint)_PersistStorageType.PST_PROJECT_FILE, out silverlightAppListValue);
MessageBox.Show(silverlightAppListValue);
}
If you still want to try doing this from an Addin, you’ll have to get follow the approach that Craig mentions to cast the DTE object to an IServiceProvider (so you can call GetService).
-Aaron Marten

Since .sln files are just text files, try editing your .sln file using Notepad. You should be able to find the property you are looking for listed there. Assuming the information is in an understandable format, you should then be able to use a simple text parser to extract the info from the .sln programmatically.

Related

Why Snippet Manager doesn't works?

I really want to use this plugin. But once I do "Export as Snippet", I can't see a new tab where I can manage the snippet, set the Shourcut and such (as shown in the video). I only see this popup:
What is going on? Add new item?
I've updated to the last (update 3) version of Visual Studio 2015, and I've Snippet Designer 1.6.2.
UPDATE - WORKAROUND, AND POSSIBLE FUTURE FIX
I have tested Snippet Designer with Web Forms (Web Site Projects), and it does not work as described in the video, i.e. with the snippet file being automatically created.
However, it is possible to continue working with Snippet Designer, by selecting the Project Item named Code Snippet, as follows:
Create or open a Web Site project.
Select the text you wish to make a Snippet. Right click and select Export as Snippet.
In the New File Dialog window that opens, you need to locate the File Type Code Snippet. The easiest way in a long list of installed File Templates is to use the search feature, typing snippet.
Click Add. The file will be added to your project, and now Snippet Designer will activate, and function as normal.
NOTE: This is creating a default C# Code Snippet file, not the snippet file that is included within the Snippet Designer .vsix archive; it makes no difference as both files are empty, and Snippet Designer has its own code for building the contents of a snippet file according to the relevant XML schema, as specified by Microsoft.
A possible reason why Snippet Designer does not automatically create the snippet file itself, as it does with other projects, is that Web Site projects use a different approach to project metadata; for example, they do not have a .csproj file, which is typically used to list the files within a project.
It could also be that the code creating the snippet file asks Visual Studio, via Automation, to create a new instance of the snippet file Item Template; on a default Visual Studio 2015 installation, the listing of available Item Templates for a Web Site Project are located in:
C:\Program Files (x86)\Microsoft Visual Studio
14.0\Web\WebNewFileItems\NewFileItems.vsdir
Snippet Designer also includes a .vsdir file that has an entry for its own bundled snippet file, the source can be viewed here:
https://github.com/mmanela/SnippetDesigner/blob/master/src/SnippetDesigner/Templates/SnippetFile.vsdir
I would suggest the author of Snippet Designer investigate if the entry here is the source of the problem.
Microsoft provide documentation for the metadata contained in .vsdir files here: Template Directory Description (.Vsdir) Files
I do actually recommend a different product. My original answer continues below:
CURRENT SITUATION
As has been documented on the Snippet Designer GitHub Issue Tracker, there are issues with specific project types not being recognised, and therefore features not available.
The last commit to the Snippet Designer GitHub project was 1 year ago (November 2015); in view of the number of unresolved issues and lack of development, I would suggest a known working open source alternative.
WORKING ALTERNATIVE
Code Snippet Studio is another free, open source project for creating, editing and managing Snippets that fully supports Visual Studio 2015 (including Update 3), and which is actively developed and released:
Code Snippet Studio
Code Snippet Studio is an extension for Visual Studio 2015 that makes it easy to create, edit, package, and share IntelliSense code snippets for Visual Studio 2015 and Visual Studio Code. For C# and Visual Basic snippets, it also provides live Roslyn code analysis as you type to immediately detect code issues.
Note that multiple languages, including C#, VB, SQL, HTML, JavaScript are supported, among others. The image below shows VB editing.
Code Snippet Studio runs as either a Tool Window in Visual Studio 2015, or, as a standalone application (which personally I find ideal for working alongside Visual Studio Code on a laptop, or to make quick updates to my snippet packages).
Features include Roslyn based intellisense and analysis while editing snippets, specification of namespaces that need to be imported to support the Snippet, the ability to export to Visual Studio or Visual Studio Code, and optionally, create a VSIX file for installation on multiple machines.
FURTHER INFORMATION
For more information, the following links are useful:
Channel 9 - A Look Behind Code Snippet Studio (with Video)
GitHub Project
Getting started Guide
Channel9 - One code snippet at a time [and more] with the Code Snippet Studio
This is a known problem, see github issues 57, 70, 84, 109. I can only suggest that you persuade addon author to fix it, or fix it yourself :)

Automatically run extension code in Visual Studio on startup

Can I create an extension for Visual Studio that runs in the background as soon as the user opens the Visual Studio IDE? For example, I am building an extension that gets the current active file address in Visual Studio (with C#), but I would like this extension to always run in the background without having to be activated by the user clicking a button or pressing some key combination.
Is this possible, and if so, what is the best way of doing it?
Any help would be greatly appreciated! Regards, Erfan
Since you tagged your question with visual-studio-2010 I assume you are working on an "Add-in" rather than a "VSPackage Extensions".
In this case, you can use the OnConnection event handler.
If you are working on a VSPackage Extensions, you can use the attribute ProvideAutoLoad.
Just search for these, you will find sufficient information. Both ways are also described shortly here under "How can I run my add-in code in a VSPackage?"
For Extension add following attribute to Package class, this will load the extension when a solution is not open in visual studio. I have tested this with VS 2015 and 2017.
[ProvideAutoLoad(UIContextGuids80.NoSolution)]
For VS 2010 and higher the recommended extensibility approach is a package (VS 2015 won't allow add-ins).
To get the package loaded when Visual Studio is loaded see HOWTO: Autoload a Visual Studio package.
Once loaded, your package may be interested in two different kind of selection change events:
To get notified when the selection in the Solution Explorer changes, get the IVsMonitorSelection interface and call the AdviseSelectionEvents/UnadviseSelectionEvents and provide a class that implements the IVsSelectionEvents interface.
To get notified when the active window changes (which can be a document window or a toolwindow), implement the IVsWindowFrameNotify interface.

View Derived Types option missing in Object Browser Settings

This MSDN article about how to display inheritance graphs in Visual Studio 2010 says there should be an "Show Derived Types" option in the Object Browser Settings and the Class View Settings. However, the option shows up in neither location in my copy of Visual Studio 2010 SP1 Ultimate. Does anybody know why and how to resolve it?
Edit
The original link is broken. Here is a link to the same article but for Visual Studio 2008 instead of 2010 (perhaps Microsoft removed the article for 2010 given that it doesn't work).
The issue is caused by the object browsing scope I selected in the Browse box. If I select Custom Component Set in the Browser box, I cannot find the “Show Derived Types” in the Object Browser Settings. But if I select the .NET Framework 4 , I can find the “Show Derived Types” in the Object Browser Settings. Which leaves me with the question of how do I see derived types for classes in my project?
Update: I've used Telerik JustDecompile (a free tool) to find derived types. However, since upgrading to Visual Studio 2015 (problem still exists) I've been using the Find All References (Shift + F12) on class constructors to help find derived types. This works well for abstract classes as calls only come from derived types, and a little more messy for non abstract classes (depending on usage).
This is not really an answer, as I cannot resolve the problem, but it allows me to include an image.
I can see derived types for mscorlib 2.0 but not mscorlib 4.0 (VS2015) when I set the scope to "All Components"; however, I can see them when I set the scope to ".NET Framework 4.0". This confirms Ɖiamond ǤeezeƦ statement that the issue is caused by the object browsing scope.

Programmatically getting solution details

Is there a a way to get the processor type, build type, etc, from a Visual Studio solution, via C#?
There is something called ConfigurationManager which takes care of the build type and other details.
Create a Visual Studio Addin (A Project Template is available in Visual Studio in the Extensibility Category) and access your current Solution Configuration with the ActionConfiguration Property from DTE.Solution. See this for further details:

How does VSTO work in Visual Studio 2010?

Few question about VSTO project I don't understand:
Registration - unlike native Office Plugin, where I've an installation project, how does the deployment work in managed add-in? After building the project, I do have the necessary entries in the registry that define the plugin (HKCU\Software\Microsoft\Office\Outlook\Addins...). Where is the code that perform this registration?
Also, why in the HKCU? I'd like the plugin to be defined for all users (in HKLM). How do I change that?
How does Visual Studio know to start Outlook, when I press F5? In the property page for the project, under the debugging tab, the 'Start Option' is set to 'Start project'. Who tells Visual Studio that starting the project means starting Outlook?
Looking for more materials about VSTO. Can you recommend a resource?
A few answers
1. Basically the same for a managed project except that the Reg entries point to the .MANIFEST file, which in turn identifies the dll of the addin assembly.
Ohhh. this is a LONG story, there's lots of info on the web about it. Google "registering an addin for all users".
Short version is that it's possible, but requires some really weird and difficult to explain registry shinanigans.
If you've created an Outlook addin property, then by virtue of that project type, VS knows what to do on start.
I've never found a good definitive source for vsto material. Google's been my best friend for that kind of info. Andrew Coates has a pretty decent list here though
http://blogs.msdn.com/b/acoat/archive/2007/08/02/vsto-resources.aspx
I have no explicit information but currently I'm working through http://msdn.microsoft.com/en-us/library/ff937654.aspx which so far has been a great source of information. Please let me know if this worked out for your Outlook project.

Resources