4 using System.Collections.Generic;
6 using Microsoft.VisualStudio;
7 using Microsoft.VisualStudio.Shell.Interop;
8 using System.Diagnostics;
9 using Microsoft.VisualStudio.Shell;
11 namespace SiliconStudio.
Paradox.VisualStudio
13 internal static class VsHelper
15 public static IVsHierarchy GetCurrentHierarchy(IServiceProvider provider)
17 EnvDTE.DTE vs = (EnvDTE.DTE)provider.GetService(typeof(EnvDTE.DTE));
if (vs == null)
throw new InvalidOperationException(
"DTE not found.");
return ToHierarchy(vs.SelectedItems.Item(1).ProjectItem.ContainingProject);
19 public static IVsHierarchy ToHierarchy(EnvDTE.Project project)
21 if (project == null)
throw new ArgumentNullException(
"project");
string projectGuid = null;
22 using (XmlReader projectReader = XmlReader.Create(project.FileName))
24 projectReader.MoveToContent();
25 object nodeName = projectReader.NameTable.Add(
"ProjectGuid");
26 while (projectReader.Read())
28 if (
Object.Equals(projectReader.LocalName, nodeName))
30 projectGuid = (
String)projectReader.ReadElementContentAsString();
break;
34 Debug.Assert(!String.IsNullOrEmpty(projectGuid));
35 IServiceProvider serviceProvider =
new ServiceProvider(project.DTE as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
return VsShellUtilities.GetHierarchy(serviceProvider,
new Guid(projectGuid));
37 public static IVsProject ToVsProject(EnvDTE.Project project)
39 if (project == null)
throw new ArgumentNullException(
"project");
40 IVsProject vsProject = ToHierarchy(project) as IVsProject;
41 if (vsProject == null)
43 throw new ArgumentException(
"Project is not a VS project.");
47 public static EnvDTE.Project ToDteProject(IVsHierarchy hierarchy)
49 if (hierarchy == null)
throw new ArgumentNullException(
"hierarchy");
50 object prjObject = null;
51 if (hierarchy.GetProperty(0xfffffffe, -2027, out prjObject) >= 0)
53 return (EnvDTE.Project)prjObject;
57 throw new ArgumentException(
"Hierarchy is not a project.");
60 public static EnvDTE.Project ToDteProject(IVsProject project)
62 if (project == null)
throw new ArgumentNullException(
"project");
63 return ToDteProject(project as IVsHierarchy);
66 public static IEnumerable<EnvDTE.Project> GetDteProjectsInSolution(IVsSolution solution)
71 foreach (var hier
in GetProjectsInSolution(solution))
73 EnvDTE.Project project = null;
77 project = ToDteProject(hier);
90 if (solution == null)
throw new ArgumentNullException(
"solution");
92 IEnumHierarchies enumHierarchies;
93 var guid = Guid.Empty;
94 solution.GetProjectEnum((uint)__VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION, ref guid, out enumHierarchies);
97 if (enumHierarchies == null)
100 var hierarchyArray =
new IVsHierarchy[1];
101 uint hierarchyFetched;
104 while (enumHierarchies.Next(1, hierarchyArray, out hierarchyFetched) == VSConstants.S_OK && hierarchyFetched == 1)
106 if (hierarchyArray[0] != null)
107 yield
return hierarchyArray[0];