关于 .net:为什么 DateTime 是 ISerializable 在 Unity C# 中返回 false? | 珊瑚贝

Why DateTime is ISerializable returns false in Unity C#?


我已经在多个 c# 版本中尝试过以下代码,并且可以正常工作:

1
2
System.DateTime date = new System.DateTime (1024);
Console.Write (date is System.Runtime.Serialization.ISerializable);

输出:

1
True

但是当我在 Unity 中运行它时它返回 false 并且代码出错:

1
2
System.DateTime date = new System.DateTime (1024);
UnityEngine.Debug.Log (date is System.Runtime.Serialization.ISerializable);

输出:

1
False

我想检测对象是否是 ISerializable,然后继续使用 GetObjectData 方法对其进行序列化,但是当我在 Unity 中的 DateTime 上尝试它时,它返回 false,而它应该返回 true,因为 DateTime 正在实现 ISerializable。

Unity 使用 .NET 3.5 和 Mono(我不知道哪个单声道版本)。

代码在 .NET 4.5 中运行良好。

在线试用

  • 你试过 date.GetType().IsSerializable 吗?
  • 我想使用 GetObjectData 方法。
  • 这个 GetObjectData 方法是什么?您还没有证明这一点,Unit 也没有(至少从我在文档中看到的内容来看)。
  • 它是从 ISerializable 接口派生的方法。 DateTime 正在实现 ISerializable 接口,但是当我尝试 date is ISerializable 它返回 false 而不是 true。
  • 所以你想通过package标准的 .NET 序列化来制作”你自己的”序列化?
  • 是的,我正在制作 Json 和二进制序列化库,我想使用 ISerializable。
  • 然后你必须重写一些不实现ISerializable、DateTime的类,例如自己实现。我没有看到任何其他选项,因为您无法重写 Unity 使用的 .NET 库。
  • 如果它会实现 ISerializable 那么它将返回 true 而你得到的是 false 。参考此页面并自行重写。那么它将是 ISerializable。
  • 你能给我一个参考链接吗?你在哪里找到它?我在您在回答中提供的链接中看不到它。
  • 您是在询问他们不同的证据吗?但是你已经有了那个证据。您已经确定 DateTime 在 Unity 中没有实现 ISerializable。这是你问题的全部前提。阅读 Container field types that can be serialized 下的 https://docs.unity3d.com/Manual/script-Serialization.html – 注意 DateTime 未在此处列出。


https://docs.unity3d.com/Manual/script-Serialization.html 声明:

Serialization rules

Serializers in Unity run in a real-time game environment. This has a
significant impact on performance. As such, serialization in Unity
behaves differently to serialization in other programming
environments. Outlined below are a number of tips on how to use
serialization in Unity.

Unity 不支持 DateTime 的序列化。

如果您需要 DateTime 序列化,那么您可能希望拥有另一个 long 属性,该属性通过 ToFileTimeUtc 和 FromFileTimeUtc 与 DateTime 保持同步。或者实现自定义序列化。


来源:https://www.codenong.com/46236767/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8889.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?