首页 > 编程语言 >c#采用toml做配置文件的坑过

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

时间:2024-04-20 20:55:25浏览次数:18  
标签:TomlTable 配置文件 c# Update value toml key table

 

这几天在玩个程序,突然看到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。

 

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

20240420

 





标签:TomlTable,配置文件,c#,Update,value,toml,key,table
From: https://www.cnblogs.com/liqi/p/18148138

相关文章

  • C语言单词记忆
    kernel内核define定义parameter参数condition条件execution执行,运行environment环境startup启动program程序****************function函数,作用called被调用main重要的,主要的return返回type类型void空argargument参数****************negative负的......
  • Computer Basics 03 - Basic Parts of a Computer
     IntroductionThebasicpartsofadesktopcomputerarethecomputercase,monitor,keyboard,mouse,andpowercord.Eachpartplaysanimportantrolewheneveryouuseacomputer.Watchthevideobelowtolearnaboutthebasicpartsofacomputer.(Video......
  • Computer Basics 03b - Mouse tutorial
     Amouseisahandhelddevicethatletsyoupointtoobjectsonthescreen,clickthem,andmovethem.Takealookatthediagrambelowtolearnthedifferentpartsofatypicalmouse.Toscrollthroughthislesson,youcanuseyourkeyboard's down......
  • Computer Basics 04 - Buttons and Ports on a Computer
     IntroductionTakealookatthefrontandbackofyourcomputercaseandcountthenumberofbuttons,ports,andslotsyousee.Nowlookatyourmonitorandcountanyyoufindthere.Youprobablycountedatleast10,andmaybealotmore.Eachcomputer......
  • 【专题STM32F103】移植STemWin5.44+ ILI9341显示无触摸屏 + cubemx+野火霸道V2
    在已经做好LCD_ ILI9841的基础上,进行移植,不考虑触摸屏。 一、无操作系统移植第一步:找到STemWin5.44库文件。 第二步:将相关文件放在Project文件夹内,设置项目相关参数。添加:GUI_X.c  GUI_config.c  LCDConf_FlexColor_Template.c 和 STemWin_CM3_wc16_ARGB.a(无OS) ......
  • 请问哪位大佬有空?我自己搞不定pycharm安装调试了?
    大家好,我是Python进阶者。一、前言前几天在Python最强王者交流群【斌】问了一个Python环境安装的问题,请问哪位大佬有空?我自己搞不定pycharm安装调试了。二、实现过程这里【东哥】也给到了一个指导,我自己也给了一个配置教程。看上去是没有加载好Python解释器,配置下就好了。......
  • Reactor并发模型
    1.Reactor模式Reactor为“反应堆”,是一种事件驱动机制。Reactor逆置了事件处理流程,应用程序需要提供相应的接口并注册到Reactor上,如果相应的时间发生,Reactor将主动调用应用程序注册的接口,这些接口称为“回调函数”。普通的函数处理机制为:调用某函数->函数执行,主程序等待阻塞-......
  • T422088 「LAOI-4」Colors
    /*手玩数据,会发现,你找不出可以进行超过两次操作的字符串,大胆假设,加上题目里怪异的k<=10^18,把k限制在2以内就没了*/#include<iostream>#include<algorithm>#include<cstring>usingnamespacestd;typedeflonglongLL;intn,len;LLm;strings;stringmake(......
  • 解决 macOS 下 Python 3.8 安装 mysqlclient 的问题
    环境背景Python版本:3.8macOS版本:14.4(M2芯片)在安装mysqlclient时遇到的问题我在网上找到的方案基本上都是通过brewinstallmysql-connector-c安装、修改mysql_config文件、安装openssl及gcc,这个解决方案对我并没有效果解决方案步骤一:配置环境变量#使用pkg-config......
  • Reflective Journal II
    (1)Becausethecharacterisspecial,firstIneedtogetfamiliarwiththecharacter,learnfromhimtogetabetterandthoroughperspective.NextIhavetodosomeresearchfromtheinternetandgetsomeinformationandsomephotostomakethepptbetter.......