1

背景: 尝试按照以下示例在 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];
4

4 回答 4

2

好的。我开始了,根据帖子,我的命名空间和程序集在 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# 类

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 
}
于 2012-07-04T06:54:57.380 回答
1

您的自定义配置部分的命名空间是什么?

我从您编写的配置文件中看到:

type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.30319.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

这不应该是这样的:

type="your.namespace.LookupMappingsConfigSection, your.assembly" ?

对于自定义配置部分,您还可以查看配置部分设计器

于 2012-07-03T07:10:27.587 回答
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>
于 2012-07-03T07:11:06.677 回答
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为适当的类型和程序集名称。

于 2012-07-03T07:17:07.213 回答