2017-10-03 28 views
9

IntermediateOutputPathをオーバーライドする新しいスタイルのcsprojプロジェクトファイルがあります。それは次のようになります。Visual Studioエクステンションと新しいcsprojフォーマットからIntermediateOutputPathを取得する方法

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

問題がある、私のVisual Studioの拡張がIntermediateOutputPathプロパティにアクセスすることはできません。 Project.Propertiesは、古いプロジェクト形式に比べてはるかに少ないものがあるようです。

私はまた、project.ConfigurationManager.ActiveConfiguration.Propertiesと同様に成功しました。

Visual Studio拡張機能からこの情報を取得する方法はありますか?

+0

の正確な値を与えますデバッグできるgithub.comのサンプルプロジェクトを共有していますか? –

+0

共有するコードはあまりありません。基本的に、サンプルVisual Studio VSIX拡張を作成します。拡張機能では、このような 'DTE'オブジェクトを取得します。' var dte =(DTE)GetService(typeof(SDTE)); '次に、新しい拡張機能をデバッグするときに、ソリューションにアクセスして 'プロジェクト'を列挙できます。 以前はすべてのMSBuildプロパティをそこから読み取ることはできましたが、もうこれはできません。 – ionoy

+0

セットアップはすべて完了しましたが、このオプションを設定できませんでしたか?どうすれば設定できますか?手動でプロジェクトを追加中にエラーが発生する –

答えて

3

だから私はすべてのプロパティ

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); 
      } 
     } 
    } 

を印刷する単純な拡張を作成し、これは出力の下に、私はあなたのよう

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

を追加したCSプロジェクトで

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 

を与えましたIntermediateOutputPathIntermediatePathとなっています。だから、使用することができます

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

編集-1 - .NET標準のプロジェクト

Output

編集-2から12 - 8月 - 2017年以上に掘りの後にそう

私がプロパティを発見した問題は、CSProject関連のプロパティではなくMSBuildプロパティなのです。そのため、[属性]属性には表示されません。これは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; 
    } 

を使用して値を取得するには少し異なった方向性を必要とし、その後はでき値

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

を取得するために、これらのメソッドを使用し、これは私の財産

+0

新しいcsproj形式をロードしていますか?新しいものは、ほとんどのプロパティとコンテンツが暗黙的に定義されているものを意味します。 – ionoy

+0

私はビジュアルスタジオ2017で新しいプロジェクトを作成しました。ほとんどのプロパティを使って作成するにはどうしたらいいですか? –

+0

NetStandardプロジェクトを作成することができます。間違いなく新しい形式を使用する必要があります。 – ionoy

関連する問題