[英]C# 6.0 Features Not Working with Visual Studio 2015
I am testing Visual Studio 2015 with C# 6.0 but the language features are not working. 我正在使用C#6.0测试Visual Studio 2015,但语言功能不起作用。 In an MVC web application, the following code does compile:
在MVC Web应用程序中,以下代码编译:
if (!string.IsNullOrWhiteSpace(Model.Profile?.TypeName))
{
// More logic here...
}
However, when I run the application via Debug and IIS Express, I get the following error: 但是,当我通过Debug和IIS Express运行应用程序时,我收到以下错误:
CS1525: Invalid expression term '.'
CS1525:无效的表达式术语'。'
How do I enable these features? 如何启用这些功能?
This works in MVC 5 (tested 5.2.3), you just need to add the roslyn code dom Nuget package 这适用于MVC 5(经过测试的5.2.3),你只需要添加roslyn代码dom Nuget包
Replacement CodeDOM providers that use the new .NET Compiler Platform ("Roslyn") compiler as a service APIs.
替换使用新的.NET编译器平台(“Roslyn”)编译器作为服务API的CodeDOM提供程序。 This provides support for new language features in systems using CodeDOM (eg ASP.NET runtime compilation) as well as improving the compilation performance of these systems.
这为使用CodeDOM的系统(例如ASP.NET运行时编译)中的新语言功能提供了支持,并提高了这些系统的编译性能。
PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/ https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/
Well, I have MVC5 and recently installed VS 2015. 好吧,我有MVC5,最近安装了VS 2015。
I've installed CodeDOM providers package, but it didn't help... But after that I realized, that package supports framework 4.5 only, while I have target framework set to 4.6 during tests - it works with 4.5 though... 我已经安装了CodeDOM提供程序包,但它没有帮助...但之后我意识到,该程序包仅支持框架4.5,而我在测试期间将目标框架设置为4.6 - 它适用于4.5虽然...
So pay attention also to target framework. 所以也要注意目标框架。 If you have 4.5 - just install package Microsoft.CodeDom.Providers.DotNetCompilerPlatform.
如果你有4.5 - 只需安装包Microsoft.CodeDom.Providers.DotNetCompilerPlatform。 But if you have 4.5.1-4.6 as a target, you will have to change in web.config section
但是如果您将4.5.1-4.6作为目标,则必须在web.config部分中进行更改
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701">
<providerOption name="CompilerVersion" value="v4.0"/>
</compiler>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
For C# , just change type to: 对于C# ,只需将类型更改为:
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
I was having this same problem in Visual Studio 2015. Another answer here alluded to the solution I used, but they incorrectly specified the fix and never gave clarification. 我在Visual Studio 2015中遇到了同样的问题。这里的另一个答案提到了我使用的解决方案,但他们错误地指定了修复程序并且从未给出澄清。
On the the Visual Studio menu, select Project and you should see the sub-item Enable C#6 / VB 14 if you are having this problem. 在Visual Studio菜单上,选择Project ,如果遇到此问题,您应该看到子项Enable C#6 / VB 14 。 Select this menu sub-item.
选择此菜单子项。 It will download the correct packages from Nuget and install them.
它将从Nuget下载正确的软件包并安装它们。 After this, restart Visual Studio and reload your solution.
在此之后,重新启动Visual Studio并重新加载您的解决方案。
I cannot verify if this will also fix the Project Properties > Build > Advanced > Language Version selection to C# 6 , so you might want to check this as well after enabling C# 6 from the menu. 我无法验证这是否也会将项目属性>构建>高级>语言版本选择修复为C#6 ,因此您可能需要在从菜单启用C#6后检查此项。
Check your project properties, go to build, advanced and see if C# 6.0 if you don't have it as default. 检查项目属性,转到构建,高级并查看C#6.0是否没有默认值。
Currently there's perfect support for MVC5 and C# 6.0 and works amazingly well! 目前对MVC5和C#6.0有完美的支持,效果非常好!
Including following the advice of installing the latest Microsoft.CodeDom.Providers.DotNetCompilerPlatform
I also had to set my root Web.config
system.codedom
to this to finally get all of the errors in Visual Studio 2015
to go away: 包括遵循安装最新的
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
的建议我还必须将我的根 Web.config
system.codedom
设置为此以最终使Visual Studio 2015
所有错误消失:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
Now restart Visual Studio
and that should do it. 现在重新启动
Visual Studio
,应该这样做。
Visual Studio 2015 will also show an Enable C#6 / VB 14
in the Project menu with an ASP.NET web site / web application selected. Visual Studio 2015还将在“项目”菜单中显示“
Enable C#6 / VB 14
,并选择“ASP.NET网站/ Web应用程序”。
This will de facto install Microsoft.CodeDom.Providers.DotNetCompilerPlatform
and Microsoft.Net.Compilers
packages into your project and add appropriate tags into web.config file. 这将事实上将
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
和Microsoft.Net.Compilers
包安装到您的项目中,并将相应的标签添加到web.config文件中。
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:7 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:15 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.