欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2024年04月22日 08:59:08   作者:glacierstream  
這篇文章主要介紹了c#采用toml做配置文件遇到的坑,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),通過本文介紹得出c#用toml文件讀取非整數(shù)字請用double,不要用float,decimal倒無所謂,反正編譯不過,切記不要用float,需要的朋友可以參考下

這幾天在玩?zhèn)€程序,突然看到c#采用圖toml文件,好用,直觀,確實(shí)也簡單。

不過。。。。。。

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類型的參數(shù)測試測試,魔咒來了。

Console.WriteLine("ServerAddress: " + toml.Get<TomlTable>("Client").Get<float>("floatXXX"));
讀取一切正常,
下一步呢?修改修改?于是看來看去有個Update函數(shù)
toml.Get<TomlTable>("Server").Update("
floatXXX
",(double)fV);
噩夢,于是1.1存進(jìn)去變成了值 1.00999999046326,怎么測試都不對,這是什么鬼
百度https://www.baidu.com/s?ie=UTF-8&tn=62095104_35_oem_dg&wd=1.00999999046326也有這個莫名其妙的數(shù)字
百思不得其解,然后下載了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));

琢磨出點(diǎn)門道來了,沒有float類型啊,于是改為double,一切風(fēng)平浪靜,回歸正常。

OMG,這個。。。。

得出個結(jié)論,c#用toml文件讀取非整數(shù)字請用double,不要用float,decimal倒無所謂,反正編譯不過,切記不要用float。

特此記錄,避免打擊迷茫,也算一個玩程序中的不太有用知識點(diǎn),算是記錄吧。

到此這篇關(guān)于c#采用toml做配置文件的坑過的文章就介紹到這了,更多相關(guān)c# toml配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論