How do I get a Project from a IVsHierarchy and viceversa
My thirteenth How do I is up.
Scenario
This is a common thing when working with the EnvDTE and Shell Interop worlds. Sometimes we want to access properties that doesn't exist in one of the worlds (like the guid property of my previous How do I) and viceversa. The focus of this How do I tackles this scenario.
Interfaces and classes needed
Code snippet
public Project GetProject(IVsHierarchy hierarchy)
{
object project;
ErrorHandler.ThrowOnFailure
(hierarchy.GetProperty(
VSConstants.VSITEMID_ROOT,
(int)__VSHPROPID.VSHPROPID_ExtObject,
out project));
return (project as Project);
}
public IVsHierarchy GetHierarchy(IServiceProvider serviceProvider, Project project)
{
var solution =
serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;
IVsHierarchy hierarchy;
solution.GetProjectOfUniqueName(project.FullName, out hierarchy);
return hierarchy;
}
Assemblies needed
- Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
- EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Stay tuned,
Pablo