当前位置: 代码网 > it编程>编程语言>C# > c#采用toml做配置文件遇到的坑

c#采用toml做配置文件遇到的坑

2024年05月26日 C# 我要评论
这几天在玩个程序,突然看到c#采用图toml文件,好用,直观,确实也简单。不过。。。。。。github上示例写的toml to tomltabletoml input file:venabledebu

这几天在玩个程序,突然看到c#采用图toml文件,好用,直观,确实也简单。

不过。。。。。。

github上示例写的

toml to tomltable

toml input file:v

enabledebug = true
[server]
timeout = 1m
[client]
serveraddress = "http://127.0.0.1:8080"

code:

var toml = toml.readfile(filename);
console.writeline("enabledebug: " + toml.get<bool>("enabledebug"));
console.writeline("timeout: " + toml.get<tomltable>("server").get<timespan>("timeout"));
console.writeline("serveraddress: " + toml.get<tomltable>("client").get<string>("serveraddress"));

output:

enabledebug: true
timeout: 00:01:00
serveraddress: http://127.0.0.1:8080

tomltable is nett's generic representation of a tomldocument. it is a hash set based data structure where each key is represented as a string and each value as a tomlobject.

using the tomltable representation has the benefit of having toml metadata - e.g. the comments - available in the data model.

很好用,于是改了个float类型的参数测试测试,魔咒来了。

console.writeline("serveraddress: " + toml.get<tomltable>("client").get<float>("floatxxx"));
读取一切正常,
下一步呢?修改修改?于是看来看去有个update函数
toml.get<tomltable>("server").update("
floatxxx
",(double)fv);
噩梦,于是1.1存进去变成了值 1.00999999046326,怎么测试都不对,这是什么鬼
百度https://www.baidu.com/s?ie=utf-8&tn=62095104_35_oem_dg&wd=1.00999999046326也有这个莫名其妙的数字
百思不得其解,然后下载了https://github.com/paiden/nett源码看看:

// values
public static result<tomlbool> update(this tomltable table, string key, bool value)
=> update(table, key, table.createattached(value));

public static result<tomlstring> update(this tomltable table, string key, string value)
=> update(table, key, table.createattached(value));

public static result<tomlint> update(this tomltable table, string key, long value)
=> update(table, key, table.createattached(value));

public static result<tomlfloat> update(this tomltable table, string key, double value)
=> update(table, key, table.createattached(value));

public static result<tomloffsetdatetime> update(this tomltable table, string key, datetimeoffset value)
=> update(table, key, table.createattached(value));

public static result<tomlduration> update(this tomltable table, string key, timespan value)
=> update(table, key, table.createattached(value));

琢磨出点门道来了,没有float类型啊,于是改为double,一切风平浪静,回归正常。

omg,这个。。。。

得出个结论,c#用toml文件读取非整数字请用double,不要用float,decimal倒无所谓,反正编译不过,切记不要用float。

特此记录,避免打击迷茫,也算一个玩程序中的不太有用知识点,算是记录吧。

到此这篇关于c#采用toml做配置文件的坑过的文章就介绍到这了,更多相关c# toml配置文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com