简体   繁体   English

如何从Visual Studio扩展和新的csproj格式获取IntermediateOutputPath

[英]How to get IntermediateOutputPath from Visual Studio extension and new csproj format

I have a new-style csproj project file that overrides IntermediateOutputPath . 我有一个新式的csproj项目文件,它覆盖了IntermediateOutputPath It looks like this: 它看起来像这样:

<PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
    <IntermediateOutputPath>new\path\to\obj</IntermediateOutputPath>
</PropertyGroup>

The problem is, my Visual Studio extension can't access IntermediateOutputPath property. 问题是,我的Visual Studio扩展无法访问IntermediateOutputPath属性。 Project.Properties seems to have much less stuff compared to old project format. 与旧的项目格式相比, Project.Properties似乎有更少的东西。

I've also tried project.ConfigurationManager.ActiveConfiguration.Properties with the similar success. 我也试过了类似成功的project.ConfigurationManager.ActiveConfiguration.Properties

Is there any way to get this information from Visual Studio extension? 有没有办法从Visual Studio扩展中获取此信息?

So I created a simple extension to print all the properties 所以我创建了一个简单的扩展来打印所有属性

    private string GetPropertiesString(Properties properties)
    {
        StringBuilder test = new StringBuilder();
        foreach (Property property in properties)
        {
            try
            {
                test.AppendLine(property.Name + ":=" + property.Value.ToString());
                Console.WriteLine(property.Name + ":=" + property.Value.ToString());
            }
            catch (Exception ex)
            {
                var x = ex.Message;
            }
        }

        return test.ToString();
    }

    private void MenuItemCallback(object sender, EventArgs e)
    {
        DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;
        var sol = dte2.Solution;
        var projs = sol.Projects;
        foreach (var proj in sol)
        {
            var project = proj as Project;
            var rows = project.ConfigurationManager.ConfigurationRowNames as IEnumerable<object>;
            foreach (var row in rows)
            {
                var config = project.ConfigurationManager.ConfigurationRow(row.ToString()).Item(1) as Configuration;
                string configs = GetPropertiesString(config.Properties);
            }
        }
    }

And this gave below output 这给出了以下输出

LanguageVersion:=
RunCodeAnalysis:=False
NoStdLib:=False
ErrorReport:=prompt
CodeAnalysisUseTypeNameInSuppression:=True
CodeAnalysisInputAssembly:=bin\Debug\WindowsFormsApp1.exe
CodeAnalysisDictionaries:=
GenerateSerializationAssemblies:=2
CodeAnalysisModuleSuppressionsFile:=GlobalSuppressions.cs
StartWorkingDirectory:=
Optimize:=False
DocumentationFile:=
StartPage:=
OutputPath:=bin\Debug\
TreatWarningsAsErrors:=False
EnableASPDebugging:=False
IncrementalBuild:=True
CodeAnalysisFailOnMissingRules:=False
CodeAnalysisLogFile:=bin\Debug\WindowsFormsApp1.exe.CodeAnalysisLog.xml
DefineConstants:=DEBUG;TRACE
UseVSHostingProcess:=True
StartProgram:=
DefineDebug:=False
CodeAnalysisIgnoreBuiltInRules:=True
CodeAnalysisRuleSetDirectories:=;F:\VS2017\Team Tools\Static Analysis Tools\\Rule Sets
CodeAnalysisCulture:=
CodeAnalysisOverrideRuleVisibilities:=False
CodeAnalysisRuleAssemblies:=
DefineTrace:=False
DebugSymbols:=True
CodeAnalysisIgnoreBuiltInRuleSets:=True
CodeAnalysisRuleSet:=MinimumRecommendedRules.ruleset
NoWarn:=
CodeAnalysisIgnoreGeneratedCode:=True
EnableSQLServerDebugging:=False
BaseAddress:=4194304
RemoteDebugEnabled:=False
StartURL:=
AllowUnsafeBlocks:=False
TreatSpecificWarningsAsErrors:=
PlatformTarget:=AnyCPU
EnableUnmanagedDebugging:=False
StartWithIE:=False
StartArguments:=
IntermediatePath:=new\path\to\obj2\
CodeAnalysisRuleDirectories:=;F:\VS2017\Team Tools\Static Analysis Tools\FxCop\\Rules
DebugInfo:=full
CheckForOverflowUnderflow:=False
RemoteDebugMachine:=
Prefer32Bit:=True
CodeAnalysisSpellCheckLanguages:=
CodeAnalysisRules:=
RegisterForComInterop:=False
FileAlignment:=512
StartAction:=0
EnableASPXDebugging:=False
ConfigurationOverrideFile:=
WarningLevel:=4
RemoveIntegerChecks:=False

In the CS project I had added 在我添加的CS项目中

<IntermediateOutputPath>new\path\to\obj2</IntermediateOutputPath>

As you can see IntermediateOutputPath is coming up as IntermediatePath . 如您所见, IntermediateOutputPath即将成为IntermediatePath So you can use 所以你可以使用

var config = project.ConfigurationManager.ConfigurationRow("Debug").Item(1) as Configuration;
config.Properties.Item("IntermediatePath").Value

Edit-1 - .NET standard project Edit-1 - .NET标准项目

产量

Edit-2 - 12-Aug-2017 编辑-2 - 2017年8月12日

So after digging more into the issue I found out the property is a MSBuild property as such and not a CSProject related property. 因此,在深入研究问题之后,我发现该属性是一个MSBuild属性,而不是CSProject相关属性。 That is why you don't see it in the Properties attribute. 这就是为什么你没有在Properties属性中看到它。 This requires a bit different direction to get the value using IVsBuildPropertyStorage.GetPropertyValue 这需要一个不同的方向来使用IVsBuildPropertyStorage.GetPropertyValue获取值

    private IVsBuildPropertyStorage GetBuildPropertyStorage(EnvDTE.Project project)
    {
        IVsSolution solution = (IVsSolution)ServiceProvider.GetService(typeof(SVsSolution));

        IVsHierarchy hierarchy;
        int hr = solution.GetProjectOfUniqueName(project.FullName, out hierarchy);
        System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr);

        return hierarchy as IVsBuildPropertyStorage;
    }

    private string GetBuildProperty(string key, IVsBuildPropertyStorage Storage)
    {
        string value;
        int hr = Storage.GetPropertyValue(key, null, (uint)_PersistStorageType.PST_USER_FILE, out value);
        int E_XML_ATTRIBUTE_NOT_FOUND = unchecked((int)0x8004C738);

        // ignore this HR, it means that there's no value for this key
        if (hr != E_XML_ATTRIBUTE_NOT_FOUND)
        {
            System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr);

        }

        return value;
    }

And then later use these methods to get the value 然后使用这些方法获取值

var project = proj as EnvDTE.Project;
IVsBuildPropertyStorage storage = GetBuildPropertyStorage(project);
string outputPath = GetBuildProperty("IntermediateOutputPath", storage);

And this gives me the correct value of the property 这给了我正确的财产价值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 项目模板支持新的 csproj 格式 - Visual Studio Project Template Support for new csproj format 使用Visual Studio 2017的新csproj格式如何添加对较旧可移植库的支持? - Using the new csproj format for Visual Studio 2017 how to add support for the older portable libraries? 如何在Visual Studio Code中以.csproj扩展名创建C#项目? - How to create C# project with .csproj extension in Visual Studio Code? 将我的 Visual Studio 扩展从旧的 csproj 移动到 SDK 样式的 csproj 后出现 NuGet 问题 - NuGet problems after moving my Visual Studio extension from old csproj to SDK-style csproj .csproj 文件中自动生成的 IntermediateOutputPath - Autogenerated IntermediateOutputPath in the .csproj file 如何在Visual Studio 2017项目(新的.csproj文件格式)中设置`OutputPath`,而不会使目标框架混乱已解析的路径? - How do I set `OutputPath` in a Visual Studio 2017 project (new .csproj file format) without the target framework cluttering the resolved path? 如何从扩展代码中获取 Visual Studio 扩展版本 - How ot get Visual studio extension version from the extension code 当混合新旧CSPROJ格式时,Visual Studio显示成千上万个参考错误,这些错误是由于单个生成错误而导致的 - Visual Studio shows thousands of reference errors that are the result of a single build error when mixing new and old CSPROJ format 如何将 Visual Studio.csproj 构建为 Mono? - How to build Visual Studio .csproj as Mono? 如何从Visual Studio Bitbucket扩展获取开发分支 - How to get a development branch from Visual Studio Bitbucket extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM