HOWTO: Get the project flavor (subtype) of a Visual Studio project from an add-in
2008-12-23 17:12
776 查看
Author: | Carlos J. Quintero (Microsoft MVP) | Applies to: | Microsoft Visual Studio 2005 |
Date: | February 2007 |
Visual Studio projects have a project type (such as VB.NET project, C# project, etc.) and each project type can have a flavor (or subtype), which is a specialized project based on the project type to customize its behavior without forcing to create a whole new project type. For example, you can have Test projects, which are based on the VB.NET or C# project types, but are specialized to create and run tests. You can learn more about project types and project subtypes in the Visual Studio 2005 SDK, sections Project Types and Project Subtypes.
Project types and subtypes are identified by Guids. The extensibility model for add-ins (EnvDTE.dll) allows you to know the Guid of the project type using the EnvDTE.Project.Kind property, but it does not allow you to get the Guid of the project subtype. This article explains how to get it using a service from the IDE.
More Information
Visual Studio stores in the .vbproj or .csproj file the project type and subtype Guids of a project. For example, if you open with a text editor a Test Project file created with VB.NET, you will find this XML entry:
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{F184B08F-C81C-45F6-A57F-5ABD9991F28F}</ProjectTypeGuids>
The list of Guids for projects types and subtypes supported by your Visual Studio installation is in this registry entry:
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/VisualStudio/8.0/Projects
where we see that the Guid {F184B08F-C81C-45F6-A57F-5ABD9991F28F} matches a VB.NET project (the project type) and the Guid {3AC096D0-A1C2-E12C-1390-A8335801FDAB} matches the Test Project subtype. See the article INFO: List of known project type Guids.
The EnvDTE.Project.Kind property returns the project type ({F184B08F-C81C-45F6-A57F-5ABD9991F28F} in this case for the Test Project) and there is no way using the extensibility model to get the project subtype. Fortunately you can use services from the IDE as explained in the article HOWTO: Get a Visual Studio service from an add-in. Specifically, we can use the Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject interface and its GetAggregateProjectTypeGuids method as shown below (VB.NET code). You will need to add references to the following assemblies (see the article HOWTO: Reference a Visual Studio assembly in the GAC from an add-in):
Microsoft.VisualStudio.OLE.Interop
Microsoft.VisualStudio.Shell.Interop
Microsoft.Visualstudio.Shell.Interop.8.0
Given an EnvDTE.Project, the following function returns a list of project type and subtype Guids separated by ";" that you can then split into individual Guids:
Public Function GetProjectTypeGuids(ByVal proj As EnvDTE.Project) As String
Dim sProjectTypeGuids As String = "" Dim objService As Object Dim objIVsSolution As Microsoft.VisualStudio.Shell.Interop.IVsSolution Dim objIVsHierarchy As Microsoft.VisualStudio.Shell.Interop.IVsHierarchy = Nothing Dim objIVsAggregatableProject As Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject Dim iResult As Integer
objService = GetService(proj.DTE, GetType(Microsoft.VisualStudio.Shell.Interop.IVsSolution)) objIVsSolution = CType(objService, Microsoft.VisualStudio.Shell.Interop.IVsSolution)
iResult = objIVsSolution.GetProjectOfUniqueName(proj.UniqueName, objIVsHierarchy)
If iResult = 0 Then
objIVsAggregatableProject = CType(objIVsHierarchy, Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject)
iResult = objIVsAggregatableProject.GetAggregateProjectTypeGuids(sProjectTypeGuids)
End If
Return sProjectTypeGuids
End Function
Public Function GetService(ByVal serviceProvider As Object, ByVal type As System.Type) As Object
Return GetService(serviceProvider, type.GUID)
End Function
Public Function GetService(ByVal serviceProvider As Object, ByVal guid As System.Guid) As Object
Dim objService As Object = Nothing Dim objIServiceProvider As Microsoft.VisualStudio.OLE.Interop.IServiceProvider Dim objIntPtr As IntPtr Dim hr As Integer Dim objSIDGuid As Guid Dim objIIDGuid As Guid
objSIDGuid = guid objIIDGuid = objSIDGuid
objIServiceProvider = CType(serviceProvider, Microsoft.VisualStudio.OLE.Interop.IServiceProvider)
hr = objIServiceProvider.QueryService(objSIDGuid, objIIDGuid, objIntPtr)
If hr <> 0 Then
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr)
ElseIf Not objIntPtr.Equals(IntPtr.Zero) Then
objService = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(objIntPtr) System.Runtime.InteropServices.Marshal.Release(objIntPtr)
End If
Return objService
End Function
相关文章推荐
- Qt Visual Studio Add-in:Adding Form Files to the Project
- How can I get the IPv4 address of an interface in linux from C code ?
- Qt Visual Studio Add-in: Creating Qt Translation Files for the Project
- This project fills the absence of stdint.h and inttypes.h in Microsoft Visual Studio
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- 在Visual Studio.NET中使用自定义插件最大化您的生产力(三)Custom Add-Ins Help You Maximize the Productivity of Visual Studio .NET
- uiview 某点返回 色值 How to get the color of a pixel in an UIView?
- 《How hard can it be? Estimating the difficulty of visual search in an image》和 PPT
- Unable to get the project file from the Web Server(in .NET 2005 IDE)
- 解决VS2010打开项目时“This project is incompatible with the current version of Visual Studio”
- Project was skipped when rebuild the solution in Visual studio
- Fast ways in R to get the first row of a data frame grouped by an identifier
- Visual Studio .Net 2003 Error: "Refreshing the project failed. Unable to retrieve folder information from the server."
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- How do I get the name of an object's type in JavaScript
- Cannot add Controls from 64-bit Assemblies to the Toolbox or Use in Designers Within the Visual Stud
- How one VBA project can use an object declared in the class module of another project
- How to create a project with existing folder of files in Visual Studio?
- How to use an external dll file in VisualStudio project