问题描述
我花了80个小时学习这种语言后才对C#陌生,现在该我迈出第一步了;练习!
因此,我正在做一个项目,使我可以测试C#的不同方面,以帮助我学习和提高知识。
所以我从一开始就攻击app.config。
我知道如何通过具有加密密码的connectionstrings标记在默认的appsetting标记以及Sql服务器连接信息中添加密钥(在这一点上,我很费劲)。
因此,我仍然必须查看在app.config文件中存储/自定义信息的部分。
我找到了一个很好的简单脚本:
class Config
{
public class MyConfigSection : ConfigurationSection
{
[ConfigurationProperty("STUFF",IsRequired = true,IsDefaultCollection = true)]
public MyConfigInstanceCollection Instances
{
get { return (MyConfigInstanceCollection)this["STUFF"]; }
set { this["STUFF"] = value; }
}
}
public class MyConfigInstanceCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new MyConfigInstanceElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
//set to whatever Element Property you want to use for a key
return ((MyConfigInstanceElement)element).Name;
}
}
public class MyConfigInstanceElement : ConfigurationElement
{
//Make sure to set IsKey=true for property exposed as the GetElementKey above
[ConfigurationProperty("name",IsKey = true,IsRequired = true)]
public string Name
{
get { return (string)base["name"]; }
set { base["name"] = value; }
}
[ConfigurationProperty("code",IsRequired = true)]
public string Code
{
get { return (string)base["code"]; }
set { base["code"] = value; }
}
}
}
我知道如何在填写完BUT后检索信息,而这就是卡住的地方,我不知道如何“填写”信息...
我希望使用上面的代码:
<configuration>
<configSections>
<section name="STUFF"
type="My.MyConfigSection,My.Assembly" />
</configSections>
<STUFF>
<add name="Arme" code="22"/>
<add name="Armure" code="12"/>
<add name="Gants" code="45"/>
</STUFF>
我缺少如何增加它的那一部分。 我试图通过创建MyConfigSection的实例来玩游戏,但我做不到。
我认为这是一个愚蠢的问题,但我似乎无法在网上找到答案,只能在填写后立即检索这些信息。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)