Search code examples
c#visual-studio-2012envdte

Can one cast an EnvDTE.Project into a VCProject


I have seen two posts so far that concern my question. I am wondering how can one cast an EnvDTE.Project into a VCProject.

In this post, fun4jimmy's answer does that exactly in the following line of code (taken from his answer) :

VCProject vcProject = project.Object as VCProject;

I have tried doing the same thing in my solution :

using EnvDTE;
using Microsoft.VisualStudio.VCProjectEngine;
[...]
private List<string> BuildAssembliesAndReturnTheirName(EnvDTE80.DTE2 dte)
{
    Solution sln = dte.Solution;

    bool isDirty = false;
    foreach (Project project in sln.Projects)
    {
        VCProject vcProject = project.Object as VCProject;
        Configuration activeConfiguration = project.ConfigurationManager.ActiveConfiguration;
        foreach (VCConfiguration vcConfiguration in vcProject.Configurations)
        {
            //business logic
        }
    }
[...]

A solution is opened in VS. The solution contains a few C# projects. Everything seems to be in order for this code to execute until I reach

foreach (VCConfiguration vcConfiguration in vcProject.Configurations) 

only to realise that this cast

VCProject vcProject = project.Object as VCProject;

returns null.

Can anyone tell me why that is? I've seen this post in which hveiras suggests

There is a VCCodeModel.dll for each VS version.

If that's the case for VCProjectEngine.dll as well, how can I fix my issue?

I have changed my reference to VCProjectEngine.dll so that it uses the one for Visual Studio 2012 (what I'm working with) but vcProject remains null.


Solution

  • VCProject is for C++ projects, in order to use a similar interface with C#/VB project you'll have to use VSProject.

    There are a number of VSLangProj overloads/extensions and you'll have to find the one that is specific to the version you need to use. See: https://msdn.microsoft.com/en-us/library/1xt0ezx9.aspx for all the VSLangProj interfaces from 2 through 100 (I think thats Version 2 through Version 10).