Pablo Galiano : How do I get a Project from a IVsHierarchy and viceversa

Subscriptions

<September 2010>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

News

Subscribe to Pablo Galiano by Email

Post Categories

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

posted on Wednesday, August 27, 2008 8:57 AM by pga

What do you think?

(required) 
required 
optional
required