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

Subscriptions

<July 2010>
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

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

# Blog Carnival #1 @ Friday, August 29, 2008 1:12 AM

I&amp;#39;ve decided to collect interesting posts on various topics and when reach critical mass publish

Anonymous