web-dev-qa-db-ja.com

タイプまたは名前空間が見つかりません「アセンブリ参照がありませんか」すべての参照が正しい

MSBuildWorkspaceクラス を使用しようとしています。プロジェクトにすべてのアセンブリ参照があります。オブジェクトブラウザで参照を開くと、使用しようとしている名前空間とクラスが表示されます。しかし、私の次のusingステートメントでは、

using Microsoft.CodeAnalysis.MSBuild

私は

The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an Assembly reference?)

しかし、シンタックスハイライトが型名を認識し、コンパイラが文句を言うのはおかしいです

enter image description here

これがビルドログです

   1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework Assembly "Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.VisualBasic.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework Assembly "Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.CSharp.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework Assembly "Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework Assembly "Microsoft.Build.Framework, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.VisualBasic.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework Assembly "Microsoft.Build.Framework, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.CSharp.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework Assembly "Microsoft.Build.Framework, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\users\fahadash\documents\visual studio 2012\Projects\RoslynEditor\RoslynEditor\MainWindow.xaml.cs(37,36,37,43): error CS0234: The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an Assembly reference?)
    1>c:\users\fahadash\documents\visual studio 2012\Projects\RoslynEditor\RoslynEditor\MainWindow.xaml.cs(37,96,37,103): error CS0234: The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an Assembly reference?)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
12
fahadash

したがって、この:

警告MSB3258:プライマリリファレンス「Microsoft.CodeAnalysis.Workspaces」は、.NET Frameworkアセンブリ「Microsoft.Build、Version = 12.0.0.0、Culture = neutral、PublicKeyToken = b03f5f7f11d50a3a」に間接的に依存しているため、解決できませんでした。現在のターゲットフレームワークのバージョン「4.0.0.0」よりも高いバージョン「12.0.0.0」。

.NET4.0フレームワークを対象としたプロジェクトで構築していることを意味します。 Visual Studio2013で4.5.1をターゲットにする必要があります。他の構成はサポートされていません。警告を消してこれを「強制」しようとすることはお勧めしません。これは、将来的に問題を引き起こす可能性があります。 Roslynは4.5で追加されたAPIを使用しているため、問題を解決しようとすると問題が発生します。

10

ナンセンからのこのブログ投稿 を見つけ、修正を適用して問題を解決しました。

解決策の概要:XMLエディターでcsprojファイルを編集し、問題のある参照の要素を見つけて、それらに次の子要素を追加します。

<SpecificVersion>True</SpecificVersion>

単語Trueが最初の文字の大文字のみであることを確認してください(True、Trueではない、またはTRUE)。

プロジェクトをVSに保存して再ロードし、ビルドします。

8
fahadash

スタートアッププロジェクト内でプロジェクト参照を使用すると、ビルドサーバーから同じビルドエラーメッセージが生成されました。VS2013IDEを使用してビルドすると正常に機能しました。

タイプまたは名前空間名 'XYZ'が見つかりませんでした(usingディレクティブまたはアセンブリ参照がありませんか?)

ビルドサーバーではなくローカルでMSBuild.exeを実行した後、エラーを再現することができました。ソリューションからアンロードしてプロジェクト参照の.csprojファイルを調べると、一番上に次の行があることに気付きました。

<Project DefaultTargets="Configure" xmlns="..." ToolsVersion="12.0">

スタートアッププロジェクトの正確な構文に従って、次の行で更新しました。

<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="...">

DefaultTargetsプロパティは同じでなければならないことがわかりました。これが、MSBuild.exeの実行後にCSC.exeコマンドを使用してプロジェクトをコンパイルしようとしたときに、プロジェクト参照が '/ reference'パラメーターに含まれていなかった理由です。

どのフォーラムでも似たようなものが見つからなかったので、この問題の解決策を見つけるのに少し時間がかかりました。これは、ビルドサーバーではなく、常にローカルでビルドされてきたレガシーアプリケーションです。

ちなみに、PSBuild(MSBuildへのPowerShellインターフェイス)をMarkdown Pad 2と一緒に使用すると、ビルドエラーのトラブルシューティングを試みるときにローカルでうまく機能します。

0
alongshot