背景 尝试按照以下示例在app.config中实现列表:
目标: 我希望解决此错误并使其正常工作。
错误:
无法识别的元素'lookupMapping'。 (C:......第75行)
调试时在此行上生成错误:
LookupMappingsConfigSection section =(LookupMappingsConfigSection)config.Sections [“lookupMappings”];
app.config片段:
<configuration>
<configSections>
<section name="lookupMappings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.30319.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
<lookupMappings>
<lookupMapping name="One" lookupName="foo" />
<lookupMapping name="Two" lookupName="foo" />
<lookupMapping name="Three" lookupName="foo" />
<lookupMapping name="Four" lookupName="foo" />
</lookupMappings>
</configuration>
类:
public class LookupMappingsInstanceElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return (string)base["name"]; }
set { base["name"] = value; }
}
[ConfigurationProperty("lookupName", IsRequired = true)]
public string LookupName
{
get { return (string)base["lookupName"]; }
set { base["lookupName"] = value; }
}
}
public class LookupMappingsConfigSection: ConfigurationSection
{
[ConfigurationProperty("lookupMappings", IsDefaultCollection = true, IsRequired = true)]
[ConfigurationCollection(typeof(LookupMappingsConfigCollection), AddItemName = "lookupMapping")]
public LookupMappingsConfigCollection Instances
{
get { return (LookupMappingsConfigCollection) this[""]; }
set { this[""] = value; }
}
}
public class LookupMappingsConfigCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new LookupMappingsInstanceElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((LookupMappingsInstanceElement) element).Name;
}
public LookupMappingsInstanceElement this[int idx]
{
get { return (LookupMappingsInstanceElement)BaseGet(idx); }
}
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override string ElementName
{
get { return "lookupMapping"; }
}
}
实施c#:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
LookupMappingsConfigSection section = (LookupMappingsConfigSection)config.Sections["lookupMappings"]; // <--ERROR ON THIS LINE
LookupMappingsInstanceElement entry1 = section.Instances[0];
LookupMappingsInstanceElement entry2 = section.Instances[1];
答案 0 :(得分:2)
行。我得到了它,根据帖子我的命名空间和程序集在app.config部分区域不正确。我还必须在我的app.config中添加一个额外的分组,以便它发挥得很好。这是我的最终产品,以防它帮助其他人..
的App.config
<configuration>
<configSection>
<section name="LookupMappingsSection" type="your.namespace.LookupMappingsConfigSection, your.assembly"/>
</configSections>
<LookupMappingsSection>
<LookupMappings>
<add name="One" lookupName="foo" />
<add name="Two" lookupName="foo" />
<add name="Three" lookupName="foo" />
<add name="Four" lookupName="foo" />
</LookupMappings>
</LookupMappingsSection>
</configuration>
c#Classes
public class LookupMappingsConfigSection: ConfigurationSection
{
[ConfigurationProperty("LookupMappings")]
public LookupMappingsConfigCollection LookupMappings
{
get { return ((LookupMappingsConfigCollection)(base["LookupMappings"])); }
}
}
[ConfigurationCollection(typeof(LookupMappingElement))]
public class LookupMappingsConfigCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new LookupMappingElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((LookupMappingElement) element).Name;
}
public LookupMappingElement this[int idx]
{
get { return (LookupMappingElement)BaseGet(idx); }
}
}
public class LookupMappingElement : ConfigurationElement
{
[ConfigurationProperty("name", DefaultValue = "",IsKey = true)]
public string Name
{
get { return (string)base["name"]; }
set { base["name"] = value; }
}
[ConfigurationProperty("lookupName", DefaultValue = "", IsKey = false, IsRequired = false)]
public string LookupName
{
get { return (string)this["lookupName"]; }
set { base["lookupName"] = value; }
}
}
//Get the lookup entries
LookupMappingsConfigSection section = (LookupMappingsConfigSection)ConfigurationManager.GetSection("LookupMappingsSection");
foreach(LookupMappingElement lookupMapping in section.LookupMappings)
{
//Do something
}
答案 1 :(得分:1)
自定义配置部分的命名空间是什么?
我从配置文件中看到你写的:
type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.30319.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
不应该是这样的:
type="your.namespace.LookupMappingsConfigSection, your.assembly" ?
对于自定义配置部分,您还可以查看Configuration Section Designer
答案 2 :(得分:1)
配置部分定义无效。它应该指向实现LookupMappingsConfigSection的完全限定名称。
<configuration>
<configSections>
<section name="lookupMappings" type="MyAssembly.Namespace.LookupMappingsConfigSection, MyAssembly"/>
</configSections>
<lookupMappings>
<lookupMapping name="One" lookupName="foo" />
<lookupMapping name="Two" lookupName="foo" />
<lookupMapping name="Three" lookupName="foo" />
<lookupMapping name="Four" lookupName="foo" />
</lookupMappings>
</configuration>
答案 3 :(得分:1)
主要问题是您尝试将lookupMappings
部分作为标准AppSettingsSection
加载。将配置文件更改为:
<configuration>
<configSections>
<section name="lookupMappings" type="Fully.Qualified.Type.Of.LookupMappingsConfigSectionType, Name.Of.Assembly.Containing.LookupMappingsConfigSectionType"/>
</configSections>
<lookupMappings>
<lookupMapping name="One" lookupName="foo" />
<lookupMapping name="Two" lookupName="foo" />
<lookupMapping name="Three" lookupName="foo" />
<lookupMapping name="Four" lookupName="foo" />
</lookupMappings>
</configuration>
注意:您需要使用相应的类型和程序集名称替换
Fully.Qualified.Type.Of.LookupMappingsConfigSectionType
和Name.Of.Assembly.Containing.LookupMappingsConfigSectionType
。