简体   繁体   English

ValueTuples可以与.net 4.5.2一起使用吗?

[英]Can ValueTuples be used with .net 4.5.2?

I have an MVC 5 project that targets .net framework 4.52 and I installed the System.ValueTuple through Nuget. 我有一个针对.net framework 4.52的MVC 5项目,我通过Nuget安装了System.ValueTuple。 I'm using Visual Studio 2017 on Windows 7. During development, Visual Studio doesnt complain about the Tuple return type but when I build I get an array of errors: 我在Windows 7上使用Visual Studio 2017.在开发过程中,Visual Studio不会抱怨Tuple返回类型,但是当我构建时,我得到一个错误数组:

error CS1519: Invalid token '(' in class, struct, or interface member declaration
error CS1519: Invalid token ',' in class, struct, or interface member declaration
error CS1519: Invalid token ')' in class, struct, or interface member declaration
error CS1520: Method must have a return type
error CS1026: ) expected
and more..

This is the method that is breaking: 这是破坏的方法:

public (bool, string) GetSettings()
{
    if (settingsFile.IsNullOrEmpty())
        throw new Exception("Settings file is null or empty!");
    try
    {
        var definition = new { active = false, excludeList = "" };
        var obj = JsonConvert.DeserializeAnonymousType(settingsFile, definition);
        return (obj.active, obj.excludeList);
    }
    catch
    {
        //TODO: log exception.
        return (false, string.Empty);
    }
}

I don't know how to fix this except to think that it is just not compatible with my framework version. 我不知道如何解决这个问题,除非认为它与我的框架版本不兼容。 Several extensive online searches have revealed nothing on this. 几项广泛的在线搜索没有透露任何相关信息。

The MSDN blog link mentioned on William Xitaras' comment's on the question already has the quick answer. 关于William Xitaras关于这个问题的评论中提到的MSDN博客链接已经有了快速的答案。

Read this paragraph on that blog : 阅读该博客上的这一段:

Note that if you don't see the ValueTuple available in your project, you have to download the System.ValueTuple 4.3.0 NuGet package to your project. 请注意,如果您没有在项目中看到ValueTuple,则必须将System.ValueTuple 4.3.0 NuGet包下载到您的项目中。 You don't need to do anything if you are using .NET Framework 4.7 or higher, or .NET Standard Library 2.0 or higher. 如果您使用的是.NET Framework 4.7或更高版本,或.NET Standard Library 2.0或更高版本,则无需执行任何操作。

Therefore You cannot use that System.ValueTuple nuget directly for .NET Framework before .NET Framework v4.7, because you have to meet some requirements first. 因此,您不能在.NET Framework v4.7之前直接为.NET Framework使用该System.ValueTuple nuget,因为您必须首先满足某些要求。 To ensure that it can work, you have to: 为了确保它可以工作,您必须:

  1. Ensure that your C# project properties has set to use correct version of C# compiler, at least C# 7.0. 确保您的C#项目属性已设置为使用正确版本的C#编译器,至少为C#7.0。 See screenshot below. 见下面的截图。
  2. Use System.ValueTuple at least version 4.4.0, because before 4.4.0 is not supported anymore, therefore it's recommended to use at least v4.4.0. 至少使用版本4.4.0的System.ValueTuple,因为不再支持4.4.0之前,因此建议至少使用v4.4.0。

To set the C# compiler version to be used, go to the project properties, select "Build" tab, click Advanced button. 要设置要使用的C#编译器版本,请转到项目属性,选择“构建”选项卡,然后单击“高级”按钮。 A list of C# compiler available will be shown like this screenshot on my VS 2017: 可用的C#编译器列表将在我的VS 2017上显示为此屏幕截图:

C#项目属性

If you change the target to .NET Framework 4.7 or later, then you should not use System.ValueTuple nuget because it is available in the runtime of .NET Framework 4.7. 如果将目标更改为.NET Framework 4.7或更高版本,则不应使用System.ValueTuple nuget,因为它在.NET Framework 4.7的运行时中可用。

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

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