简体   繁体   English

旧项目无法识别lambda表达式

[英]Old project doesn't recognise lambda expression

I have an old project that was using .net 4.5 which I have upgraded to .net 4.6.2. 我有一个使用.net 4.5的旧项目,该项目已升级到.net 4.6.2。 I am using visual studio 2017, so it should support lambda expressions, but when I try to run my project it always throws this error: 我正在使用Visual Studio 2017,因此它应该支持Lambda表达式,但是当我尝试运行我的项目时,它总是会引发以下错误:

Invalid token '=>' in class, struct, or interface member declaration 类,结构或接口成员声明中的令牌'=>'无效

How can I get my project to compile using c# 6? 如何使用c#6编译项目? I thought just upgrading the target framework was enough? 我认为仅升级目标框架就足够了吗?

You need to set the Advanced Build Settings most likely 您最有可能需要设置“ 高级构建设置”

RightClick Project -> Properties -> Build -> Advanced build settings

Set the Language Version to your favorite or something appropriate Language Version设置为您喜欢的Language Version或适当的Language Version

在此处输入图片说明

Try Adding the reference for Microsoft.Net.Compilers to the said project using Nuget (specific for 2.10 or above) to force compiler bindings to use 4.6+ features. 尝试使用Nuget(特定于2.10或更高版本)将Microsoft.Net.Compilers的引用添加到所述项目中,以强制编译器绑定使用4.6+功能。

Invalid token class error usually appears when .Net Compilers are not correctly configured in Project. 当未在Project中正确配置.Net编译器时,通常会出现无效的令牌类错误。

Install-Package Microsoft.Net.Compilers -Version 2.10.0

Note: If you're using VS 2017, it should automatically do so. 注意:如果您使用的是VS 2017,它应该会自动使用。

By changing the default language version, I started getting another error: 通过更改默认语言版本,我开始收到另一个错误:

Invalid option '7' for /langversion; / langversion的选项'7'无效; must be ISO-1, ISO-2, Default or an integer in range 1 to 6 必须为ISO-1,ISO-2,默认值或1到6之间的整数

I investigated this and found some answers, which lead me to the correct answer. 我对此进行了调查,找到了一些答案,这些答案使我得出了正确的答案。 The only thing I had to do was update the package 我唯一要做的就是更新软件包

Microsoft.Net.Compilers Microsoft.Net。编译器

It was using version 1.3 , I upgraded to 2.10.0 and everything worked. 它使用的是1.3版,我升级到2.10.0 ,一切正常。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM