How to: Verify Subtypes of a Project at Run Time

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.