0

そのため、app-config ファイルから情報を読み取るカスタム セクション ハンドラーを作成しましたが、実行しようとすると、次のエラーが発生します: オブジェクト参照がオブジェクトのインスタンスに設定されていません。

私はこの問題を2日間解決しようとしていますが、運が悪いです

ここに私のコードを見てください: アプリ - 設定

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- Configuration section-handler declaration area. -->
  <configSections>
        <sectionGroup name="propertyValuesGroup">
          <section
            name="propertyValues"
            type="FlatFileTestCaseAutomater.ClaimHeaderSection,FlatFileFactory"
            allowLocation="true"
            allowDefinition="Everywhere"
          />
        </sectionGroup>
    <!-- Other <section> and <sectionGroup> elements. -->
  </configSections>

  <!-- Configuration section settings area. -->

  <propertyValuesGroup>

    <propertyValues>
      <property>
        <clear/>
      <claimHeader name="txnNo" nullable="yes" dataType="int" maxLength="20" />
      <claimHeader name="batchNo" nullable="yes" dataType="string" maxLength="20" />
      </property>
    </propertyValues>

  </propertyValuesGroup>


</configuration>

私のカスタム セクション ハンドラ:

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;

namespace FlatFileTestCaseAutomater
{
    class ClaimHeaderSection : ConfigurationSection
    {

        [ConfigurationProperty("claimHeader")]
        public ClaimHeaderElement ClaimHeaderProperty
        {
            get
            {
                return (ClaimHeaderElement)this["claimHeader"];
            }
            set
            { this["claimHeader"] = value; }
        }
    }





    public class ClaimHeaderElement : ConfigurationElement
    {
        [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
        public String Name
        {
            get
            {
                return (String)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("nullable", DefaultValue = "yes", IsRequired = true)]
        public String Nullable
        {
            get
            {
                return (String)this["nullable"];
            }
            set
            {
                this["nullable"] = value;
            }
        }

        [ConfigurationProperty("dataType", DefaultValue = "", IsRequired = true)]
        public String DataType
        {
            get
            {
                return (String)this["dataType"];
            }
            set
            {
                this["dataType"] = value;
            }
        }

        [ConfigurationProperty("maxLength", DefaultValue = "", IsRequired = true)]
        public string MaxLength
        {
            get
            { return (string)this["maxLength"]; }
            set
            { this["maxLength"] = value; }
        }

    }

}

そして、オブジェクトを呼び出します:

 FlatFileTestCaseAutomater.ClaimHeaderSection config =
    (FlatFileTestCaseAutomater.ClaimHeaderSection)System.Configuration.ConfigurationManager.GetSection(
    "propertyValuesGroup/propertyValues/property/");
4

1 に答える 1

1

セクション パスが間違っているため、null を取得しています。次のようにする必要があります。

ClaimHeaderSection config =
(ClaimHeaderSection)System.Configuration.ConfigurationManager.GetSection(
"propertyValuesGroup/propertyValues");

ただし、カスタム構成セクションを設計したので、その有効なパスを入力すると、「認識されない要素 'property'」というメッセージとともに ConfigurationErrorsException が発生します。

投稿した構成に基づいて、ClaimHeaderElement のコレクションが必要ですが、カスタム セクションでConfigurationElementCollectionを定義していません。代わりに、ConfigurationElement を定義しています。

動作させるには、次のような ConfigurationElementCollection を実装する必要があります。

public class ClaimHeaderCollection : ConfigurationElementCollection 
{

    protected override ConfigurationElement CreateNewElement()
    {
        return new ClaimHeaderElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ClaimHeaderElement)element).Name;
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }
}

カスタム セクションで、コレクションを追加する必要があります。

public class ClaimHeaderSection : ConfigurationSection
{
    [ConfigurationProperty("property", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(ClaimHeaderCollection), 
        AddItemName = "add",
        ClearItemsName = "clear",
        RemoveItemName = "remove")]
    public ClaimHeaderCollection ClaimHeaders
    {
        get
        {
            return (ClaimHeaderCollection)this["property"];
        }
        set
        { this["property"] = value; }
    }

    public static ClaimHeaderSection GetConfiguration()
    {
        return GetConfiguration("propertyValuesGroup/propertyValues");
    }

    public static ClaimHeaderSection GetConfiguration(string section) 
    {
        return ConfigurationManager.GetSection(section) as ClaimHeaderSection;
    }
}

GetConfiguration という 2 つの静的メソッドを追加したことに注意してください。これにより、構成セクションの取得が容易になります。

ClaimHeaderSection config = ClaimHeaderSection.GetConfiguration();

構成で、構成要素の名前を claimHeader から add に変更する必要があります。xml ノードを add ではなく claimHeader と呼びたい場合は、ConfigurationCollection 属性の AddItemName 値を add から claimHeader に変更する必要があります。属性に add を入れたので、「add」のままにします。

<!-- Configuration section settings area. -->
<propertyValuesGroup>
  <propertyValues>
    <property>
      <clear/>
      <add name="txnNo" nullable="yes" dataType="int" maxLength="20" />
      <add name="batchNo" nullable="yes" dataType="string" maxLength="20" />
    </property>
  </propertyValues>
</propertyValuesGroup>
于 2013-04-30T23:55:09.373 に答える