我尝试使用ConfigurationManager来配置我的C#应用程序。CM只能做键值对吗?喜欢与
<add key="k1" value="v1"/>
或者我也可以使用诸如
<God Name="Shiva" Spouse="Parvati" Legs="2 Arms="6"/>
您可以使用自定义配置部分:https : //docs.microsoft.com/zh-cn/dotnet/api/system.configuration.configurationsection?view=dotnet-plat-ext-5.0
替换CustomSection
为God
,您就可以设置。当然,您需要编写额外的代码才能读取它们,但这是完全可行的。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="CustomSection" type="Samples.AspNet. CustomSection, CustomConfigurationSection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" />
</configSections>
<CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />
</configuration>
另一个很好的例子可以在这里找到:https : //www.jokecamp.com/blog/net-custom-configuration-section-collection-and-elements/
不过要当心。配置应用于应用程序常数。上帝及其能力看起来更适合数据源(尽管我想这只是一个例子)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句