Verifying Subtypes of a Project at Run Time


This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2019. Download it here

A VSPackage that depends on a custom project subtype should include logic to look for that subtype so that it can fail gracefully if the subtype is not present. The following procedure shows how to verify the presence of a specified subtype.

To verify the presence of a subtype

  1. Obtain the project hierarchy from the project and solution objects as a IVsHierarchy object by adding the following code to your VSPackage.

    EnvDTE.DTE dte;  
    dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));  
    EnvDTE.Project project;  
    project = dte.Solution.Projects.Item(1);  
    IVsSolution solution;  
    solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution));  
    IVsHierarchy hierarchy;  
    hierarchy = solution.GetProjectOfUniqueName(project.UniqueName);  
  2. Cast the hierarchy to the IVsAggregatableProjectCorrected interface.

    IVsAggregatableProjectCorrected AP;  
    AP = hierarchy as IVsAggregatableProjectCorrected;  
  3. Get the list of project type GUIDs by invoking the GetAggregateProjectTypeGuids.

    string projTypeGuids = AP.GetAggregateProjectTypeGuids().ToUpper();  
  4. Check the list for the GUID of the specified subtype.

    // Replace the string "MyGUID" with the GUID of the subtype.  
    string guidMySubtype = "MyGUID";  
    if (projTypeGuids.IndexOf(guidMySubtype) > 0)  
        // The specified subtype is present.  

See Also

Project Subtypes
Project Subtypes Design
Properties and Methods Extended by Project Subtypes