gpt4 book ai didi

c# - 迁移到 VS 2017 后缺少 ToUnixTimeSeconds 方法

转载 作者:太空狗 更新时间:2023-10-29 20:51:12 29 4
gpt4 key购买 nike

var item = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();

该代码导致编译错误“不包含 ToUnixTimeSeconds 的定义...”。它在 VS 2015 中运行良好,我也有 using System; 命名空间和对 mscorlib 和 System(4.0.0.0 版本)的引用。存在 DateTimeOffset 的许多其他成员。

最佳答案

(根据 Eric 的评论进行编辑)

.NET Framework 4.6 中添加了以下 API;来自 release notes :

您还可以查看官方文档中的“适用于”部分以确认兼容性:https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tounixtimeseconds#applies-to

只需在项目文件 (.csproj) 中将您的应用程序目标框架更新为框架版本 4.6 或更高版本:

<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<!--
or multiple frameworks at once:
<TargetFrameworks>net46,netstandard1.3</TargetFrameworks>
-->
...
</PropertyGroup>

然后在 C# 代码中:

public static long UnixTimeNowSec => DateTimeOffset.Now.ToUnixTimeSeconds();

关于c# - 迁移到 VS 2017 后缺少 ToUnixTimeSeconds 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43606782/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com