1

背景: 次の例に従って app.config にリストを実装しようとしています:

目標: このエラーを解決して、これを機能させたいと考えています。

エラー:

要素 'lookupMapping' を認識できません。(C: ...75行目)

以下のデバッグ中にこの行で生成されたエラー:

LookupMappingsConfigSection セクション = (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 を適切に再生するには、別のグループを追加する必要がありました。他の誰かを助ける場合に備えて、これが私の最終製品です..

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

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 andName.Of.Assembly.Containing.LookupMappingsConfigSectionTypeを適切な型とアセンブリ名に置き換える必要があります。

于 2012-07-03T07:17:07.213 に答える
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もチェックしてください。

于 2012-07-03T07:10:27.587 に答える
1

config セクションの定義が無効です。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 に答える