Old project doesn't recognise lambda expressio

2019-08-25 14:32发布

I have an old project that was using .net 4.5 which I have upgraded to .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:

Invalid token '=>' in class, struct, or interface member declaration

How can I get my project to compile using c# 6? I thought just upgrading the target framework was enough?

3条回答
Viruses.
2楼-- · 2019-08-25 14:55

By changing the default language version, I started getting another error:

Invalid option '7' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 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

It was using version 1.3, I upgraded to 2.10.0 and everything worked.

查看更多
萌系小妹纸
3楼-- · 2019-08-25 15:03

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

enter image description here

查看更多
beautiful°
4楼-- · 2019-08-25 15:06

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.

Invalid token class error usually appears when .Net Compilers are not correctly configured in Project.

Install-Package Microsoft.Net.Compilers -Version 2.10.0

Note: If you're using VS 2017, it should automatically do so.

查看更多
登录 后发表回答