代码之家  ›  专栏  ›  技术社区  ›  Andrus

清除NuGet缓存后,LanguageVersion不包含C#9的定义

  •  0
  • Andrus  · 技术社区  · 3 年前

    创建ASP。NET Core 6 MVC应用程序,带有Visual Studio 2022中的EF Core和Npgsql。

    在中单击“清除所有NuGet缓存”按钮后

    Visual Studio > Tools Options > NuGet Package Manager > General
    

    enter image description here

    属性Microsoft。代码分析。CSharp。语言版本。源代码中的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
        }
    }
    

    微软代码分析。CSharp.dll文件不是从任何anyprojet直接引用的。

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

    0 回复  |  直到 3 年前
        1
  •  1
  •   julealgon    3 年前

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

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

    如果无法手动升级,请检查项目依赖项树上的间接依赖项,然后升级顶部引用旧包的任何包。您可以通过在解决方案资源管理器中浏览项目的依赖项节点中的依赖项来完成此操作。

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