【问题标题】:LanguageVersion does not contain a definition for C# 9 after clearing NuGet caches清除 NuGet 缓存后,LanguageVersion 不包含 C# 9 的定义
【发布时间】:2022-01-21 11:45:57
【问题描述】:

在 Visual Studio 2022 中使用 EF Core 和 Npgsql 创建 ASP.NET Core 6 MVC 应用程序。

点击“清除所有 NuGet 缓存”按钮后

Visual Studio > Tools Options > NuGet Package Manager > General

源代码中的属性 Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp9

CSharpParseOptions.Default.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp9);

抛出编译错误

错误 CS0117 'LanguageVersion' 不包含 'CSharp9'

所有 NuGet 包都是最新的。如何解决这个问题?程序集信息不包含 CSharp9 成员:

#region Assembly Microsoft.CodeAnalysis.CSharp, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// location unknown
// Decompiled with ICSharpCode.Decompiler 6.1.0.5902
#endregion

namespace Microsoft.CodeAnalysis.CSharp
{
    //
    // Summary:
    //     Specifies the language version.
    public enum LanguageVersion
    {
        //
        // Summary:
        //     C# language version 1
        CSharp1 = 1,
        //
        // Summary:
        //     C# language version 2
        CSharp2 = 2,
        //
        // Summary:
        //     C# language version 3
        //
        // Remarks:
        //     Features: LINQ.
        CSharp3 = 3,
        //
        // Summary:
        //     C# language version 4
        //
        // Remarks:
        //     Features: dynamic.
        CSharp4 = 4,
        //
        // Summary:
        //     C# language version 5
        //
        // Remarks:
        //     Features: async, caller info attributes.
        CSharp5 = 5,
        //
        // Summary:
        //     C# language version 6
        //
        // Remarks:
        //     Features:
        //     • Using of a static class
        //     • Exception filters
        //     • Await in catch/finally blocks
        //     • Auto-property initializers
        //     • Expression-bodied methods and properties
        //     • Null-propagating operator ?.
        //     • String interpolation
        //     • nameof operator
        //     • Dictionary initializer
        CSharp6 = 6,
        //
        // Summary:
        //     C# language version 7.0
        //
        // Remarks:
        //     Features:
        //     • Out variables
        //     • Pattern-matching
        //     • Tuples
        //     • Deconstruction
        //     • Discards
        //     • Local functions
        //     • Digit separators
        //     • Ref returns and locals
        //     • Generalized async return types
        //     • More expression-bodied members
        //     • Throw expressions
        CSharp7 = 7,
        //
        // Summary:
        //     C# language version 7.1
        //
        // Remarks:
        //     Features:
        //     • Async Main
        //     • Default literal
        //     • Inferred tuple element names
        //     • Pattern-matching with generics
        CSharp7_1 = 701,
        //
        // Summary:
        //     C# language version 7.2
        //
        // Remarks:
        //     Features:
        //     • Ref readonly
        //     • Ref and readonly structs
        //     • Ref extensions
        //     • Conditional ref operator
        //     • Private protected
        //     • Digit separators after base specifier
        //     • Non-trailing named arguments
        CSharp7_2 = 702,
        //
        // Summary:
        //     C# language version 7.3
        CSharp7_3 = 703,
        //
        // Summary:
        //     C# language version 8.0
        CSharp8 = 800,
        //
        // Summary:
        //     The latest major supported version.
        LatestMajor = 2147483645,
        //
        // Summary:
        //     Preview of the next language version.
        Preview = 2147483646,
        //
        // Summary:
        //     The latest supported version of the language.
        Latest = int.MaxValue,
        //
        // Summary:
        //     The default language version, which is the latest supported version.
        Default = 0
    }
}

Microsoft.CodeAnalysis.CSharp.dll 文件没有被任何 anyprojet 直接引用。

NuGet 窗口不显示任何名称中包含 roslyn 的包。

【问题讨论】:

  • 您的项目使用哪种语言版本?
  • 你试过重启VS吗?如果您的项目支持它,请先尝试从命令行构建(使用您用于命令行构建的任何内容)
  • 看起来 Ef Core Npgsql 提供程序需要 9,因为使用 8 脚手架会引发错误CS8400: Feature 'or pattern' is not available in C# 8.0. Please use language version 9.0 or greater. 重新启动 Visual Studio 后,选择干净的项目并重建所有问题仍然存在。如何从命令行构建它?
  • 那么做两件事:确保在您的项目中也使用语言版本 9(或更高版本)。 (在项目文件中设置<LangVersion>9</LangVersion>)。大多数(较大的)项目都准备好使用一些特殊的构建工具(例如 Nuke)从命令行构建。如果您不知道那是什么,您可以尝试标准的 .NET 构建命令:dotnet build <YourSolution>
  • 从命令行构建解决方案后问题仍然存在。看起来这个 dll 文件在解决方案中有两个版本。我更新了问题。如何删除旧版本?

标签: c# visual-studio npgsql roslyn-code-analysis csharpcodeprovider


【解决方案1】:

该错误清楚地表明您使用的是旧版本的 DLL,其中包含 C#9 可用之前的枚举。

只需升级项目中的包以使用最新的包,它应该可以正常工作。这是该软件包的最新版本:

如果无法手动升级,请检查项目依赖树上的间接依赖关系,并升级顶部引用旧包的任何包。您可以通过在解决方案资源管理器中浏览项目依赖项节点中的依赖项来做到这一点。

另一种查找确切引用“有问题”(在本例中为旧)dll 的项目的方法是通过解决方案文件夹中的 DLL 名称进行 Windows 搜索,并检查文件大小的差异:通常,不同的版本会有不同的大小,这样您就可以在您的项目中以及与 NuGet 中的包进行比较。

【讨论】:

  • 解决方案不包含对此包的任何引用。 Microsoft.CodeAnalysis.CSharp.dll 仅在项目的 bin\Debug\net6.0 文件夹中。我删除了 bin 和 obj 文件夹,重新构建从未知位置加载此 dll 并放入 bin\Debug\net6.0 文件夹。如何升级这个包?
  • @Andrus 这可能有点困难,但你必须找到“什么包带来了这个依赖”:它被安装到你的项目中。一旦你更新了 that 包,它也应该更新传递依赖。或者,您可以在失败的项目中显式安装它,它应该选择您安装的版本而不是传递版本。这可能更容易,但没有那么干净。
  • @Andrus 另一个检查的地方是进入整个解决方案的 Nuget 包管理器(右键单击解决方案 -> 管理 Nuget 包以获得解决方案)。检查“合并”选项卡中的任何内容(最好不要在那里有任何内容)。然后,检查挂起的更新,看看您的解决方案中是否有一些严重过时的包,然后尝试更新它们。检查 Razor 相关依赖项、分析器依赖项以及任何可能使用 CodeAnalysis.CSharp 的内容,并更新这些内容。
猜你喜欢
  • 2023-03-23
  • 2011-06-07
  • 1970-01-01
  • 2015-09-05
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
相关资源
最近更新 更多