f# - F# 读取自定义配置并将其表示为带有 ConfigurationElements 的 ConfigurationSection
问题描述
我对 F# 比较陌生,但我对创建和读取自定义配置文件有疑问。我知道它在 c# 中的样子,所以例如我有一个简单的配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="customSection" type="Tool.Lib.Config, Tool.Lib" />
</configSections>
<communicationSection>
<responseTimeoutInMs value="900000" />
</communicationSection>
</configuration>
基于c#中的它很简单。我正在创建名为 Config 的模型,其属性标记为 ConfigurationProperty(女巫与 xml 节点名称相关,在本例中为 responseTimeoutInMs),例如:
[ConfigurationProperty("responseTimeoutInMs")]
public ResponseTimeoutConfigElement ResponseTimeoutInMs
{
get => (ResponseTimeoutConfigElement)base["responseTimeoutInMs"];
set => base["responseTimeoutInMs"] = value;
}
当然 value 被设置为 ConfigurationElement 所以:
public class ResponseTimeoutConfigElement : ConfigurationElement
{
[ConfigurationProperty("value", IsRequired = true, IsKey = true, DefaultValue = 0)]
public int Value => (int)base["value"];
}
这是一个很好的机制,我可以将转换器固定在其中并在读取配置时创建我需要的类型。
我知道我可以使用 ConfigurationManager 和 exe 配置映射读取默认配置,但这是使用键和值读取的基本配置。
所以我的问题是,F# 中是否有与 C# 中类似的东西?
解决方案
我不确定它是否完全符合您提到的使用“自定义配置文件”的要求,但过去我使用AppSettings 类型提供程序来获得对应用程序变量的强类型访问。
如果这不合适,还有一个更标准的XML 类型提供程序可能会有所帮助吗?
我发现类型提供程序在避免为简单访问而编写样板代码方面非常有用,它们是 F# 开发的一个非常好的特性。
推荐阅读
- bash - 将别名命令从 tcsh 转换为 bash
- c# - 将附加属性绑定到控件模板内的附加属性
- python - RGB 取自 ImageGrab.grab().load() 是在数组还是字符串中
- python - 时间列表中的处理日期
- rest - 如何保存表单集合(CollectionType)
- nstableview - NSTableView 在非 Retina 屏幕上带有模糊的 NSTextField
- javascript - 将整个 Redux 状态对象传递给 React 组件可以吗?
- python - 跳过 Elif 条件
- python - 无效语法:带有 Python 的 Azure CLI
- python - 使用 geckodriver 的 Firefox v67 私人浏览并不总是启用插件