我必须在根设置组中保存 2 组不同的设置。它应该看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_1">
........................
some_settings
........................
</sectionGroup>
<sectionGroup name="GROUP_2">
........................
some_other_settings
........................
</sectionGroup>
</sectionGroup>
</configSections>
................................
other_system_tags
................................
</configuration>
细微差别是我必须在我的代码中的不同地方一个接一个地保存它。 (例如,GROUP_1 可以是连接字符串,GROUP_2 是一些环境设置,它们一起由用户在我的应用程序的不同部分填充)
我制作了这个简单的测试类以获得预期的结果
[TestFixture]
public class Tttt
{
private string ROOT_GROUP = "ROOT_GROUP";
private string GROUP_1 = "GROUP_1";
private string GROUP_2 = "GROUP_2";
[Test]
public void SaveSettingsGroups()
{
SaveGroup1();
SaveGroup2();
Assert.True(true);
}
private Configuration GetConfig()
{
var configFilePath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
var map = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
return config;
}
private void SaveGroup1()
{
var config = GetConfig();
var root = new UserSettingsGroup();
config.SectionGroups.Add(ROOT_GROUP, root);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
var nested = new UserSettingsGroup();
root.SectionGroups.Add(GROUP_1, nested);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(nested.Name);
}
private void SaveGroup2()
{
var config = GetConfig();
var root = config.GetSectionGroup(ROOT_GROUP);
var nested = new UserSettingsGroup();
root.SectionGroups.Add(GROUP_2, nested);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(nested.Name);
}
}
但由于某些原因,这段代码的结果不同
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_1">
........................
some_settings
........................
</sectionGroup>
</sectionGroup>
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_2">
........................
some_other_settings
........................
</sectionGroup>
</sectionGroup>
</configSections>
................................
other_system_tags
................................
</configuration>
ROOT_GROUP 节点是重复的,当然 visual studio 抛出一个 ROOT_GROUP 已经存在的异常。显然,当我将新的嵌套组添加到现有根组然后保存它时,我的问题隐藏在方法 SaveGroup2() 中 - 但为什么呢?
UPD 我刚刚添加了新方法
private void SaveGroup3()
{
var config = GetConfig();
var root = config.GetSectionGroup(ROOT_GROUP);
var nested1 = root.SectionGroups.Get(0);
var nested2 = new UserSettingsGroup();
var nested3 = new UserSettingsGroup();
nested1.SectionGroups.Add("GROUP_2", nested2);
root.SectionGroups.Add("GROUP_3", nested3);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(nested2.Name);
ConfigurationManager.RefreshSection(nested3.Name);
}
并在测试中替换它
[Test]
public void SaveSettingsGroups()
{
SaveGroup1();
SaveGroup3();
Assert.True(true);
}
得到了这个奇怪的行为
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_1">
<sectionGroup name="GROUP_2">
</sectionGroup>
</sectionGroup>
<sectionGroup name="GROUP_3">
</sectionGroup>
</sectionGroup>
如您所见,奇怪之处在于结果完全在意料之中。 ROOT_GROUP 不是重复的,因为我需要它,但为什么它在 SaveGroup2() 中呢?我是否遗漏了 SaveGroup2() 中的某些内容?
UPD2 - 破解
只是尝试了一个简单的想法 - 如果我在向其添加新的嵌套元素之前清除 root_group 会怎样?
private void SaveGroup2()
{
var config = GetConfig();
var root = config.GetSectionGroup(ROOT_GROUP);
var nested = new ConfigurationSectionGroup();
//Copy exiting nested groups to array
var gr = new ConfigurationSectionGroup[5];
root.SectionGroups.CopyTo(gr,0);
gr[1] = nested;
//<!----
root.SectionGroups.Clear();
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
root.SectionGroups.Add(gr[0].Name, gr[0]);
root.SectionGroups.Add(GROUP_2, gr[1]);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
}
你怎么猜 - 它有效!
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_1" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
<sectionGroup name="GROUP_2" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
</sectionGroup>
</sectionGroup>
我认为它看起来像是一个错误,或者我遗漏了一些隐藏的东西。有人可以解释我做错了什么吗?
最佳答案
我花了一段时间才弄清楚发生了什么事,tl;dr 对我来说,框架代码本身似乎存在问题,特别是 method WriteUnwrittenConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) 类 MgmtConfigurationRecord 内。我不想写很长的故事,但如果您愿意,可以调试 .Net 框架代码并亲眼看看。
您可以通过以下方式修复您的代码:
<强>1。将所有组一起保存
private void SaveGroups()
{
var config = GetConfig();
var root = new ConfigurationSectionGroup();
config.SectionGroups.Add(ROOT_GROUP, root);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
var nested = new UserSettingsGroup();
root.SectionGroups.Add(GROUP_1, nested);
nested = new UserSettingsGroup();
root.SectionGroups.Add(GROUP_2, nested);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
}
<强>2。在添加新项目之前删除现有的组项目
private void SaveGroup2()
{
var config = GetConfig();
var root = config.SectionGroups[ROOT_GROUP];
var existingGroups = new Dictionary<string, ConfigurationSectionGroup>();
while (root.SectionGroups.Count > 0)
{
existingGroups.Add(root.SectionGroups.Keys[0], root.SectionGroups[0]);
root.SectionGroups.RemoveAt(0);
}
config.Save(ConfigurationSaveMode.Modified);
existingGroups.Add(GROUP_2, new UserSettingsGroup());
foreach (var key in existingGroups.Keys)
{
existingGroups[key].ForceDeclaration(true);
root.SectionGroups.Add(key, existingGroups[key]);
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(root.Name);
}
强>强>关于c# - App.config 将嵌套组添加到现有节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52344542/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?