Can one cast an EnvDTE.Project into a VCProject

2019-05-29 08:10发布

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.

2条回答
祖国的老花朵
2楼-- · 2019-05-29 08:52

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).

查看更多
再贱就再见
3楼-- · 2019-05-29 09:06

You can't cast the Project object itself, because there's no inheritance relationship.
But you can use the inner object:

VCProject vcProject = project.Object as VCProject;
查看更多
登录 后发表回答