一个的DateTimeOffset设置时,当我保存在我的项目设置的DateTimeOffest
失去精度,我失去了一些精度: 节省项目设置
第一个变量是原始值,系列化之前。 第二个是反序列化后的值。
其实我的变量序列化这样的配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<MyApp.Properties.Settings>
[...]
<setting name="LatestCheckTimestamp" serializeAs="String">
<value>02/22/2013 14:39:06 +00:00</value>
</setting>
[...]
</MyApp.Properties.Settings>
</userSettings>
</configuration>
有没有指定某些系列化参数来提高精度的方法吗?
我知道我可以使用一些解决方法,例如通过存储Ticks和偏移值或类似的东西,但我想知道是否没有更好的方法。
编辑: 更多信息:我使用的是标准的Visual Studio项目设置存储我的价值:
MyApp.Settings.Default.LatestCheckTimestamp = initialLatestCheckTimestamp;
MyApp.Settings.Default.Save();
MyApp.Settings
是当你在项目属性编辑设置由Visual Studio生成的类页。
编辑2:解决方案:
基础上马特·约翰逊的答案,这是我做过什么:
- 改名从
LatestCheckTimestamp
设置以LatestCheckTimestampString
但没有在我的代码 - 在独立文件中添加以下包装以完成部分类
Settings
:
。
public DateTimeOffset LatestCheckTimestamp
{
get { return DateTimeOffset.Parse(LatestCheckTimestampString); }
set { LatestCheckTimestampString = value.ToString("o"); }
}
新的配置文件如下:
<configuration>
<userSettings>
<MyApp.Properties.Settings>
[...]
<setting name="LatestCheckTimestampString" serializeAs="String">
<value>2013-02-22T16:54:04.3647473+00:00</value>
</setting>
</MyApp.Properties.Settings>
</userSettings>
</configuration>
...和我的代码仍然是
MyApp.Settings.Default.LatestCheckTimestamp = initialLatestCheckTimestamp;
MyApp.Settings.Default.Save();
您打算支持多个Windows用户吗?如果是这样,每个人都有自己的“上次检查时间”,或在他们之间共享? ApplicationSettingsBase(Settings类的派生类)存在的问题之一是它不允许写入应用程序作用域设置(尽管有解决方法)。 – jerry 2013-02-22 18:40:35
@jerry我的设置是用户范围。没有问题。感谢您的询问。 – JYL 2013-02-22 21:27:17