这几天在玩个程序,突然看到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配置文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论