C# 应用程序配置,自定义节处理程序对象返回为null

C# 应用程序配置,自定义节处理程序对象返回为null,c#,app-config,nullreferenceexception,configurationsection,C#,App Config,Nullreferenceexception,Configurationsection,因此,我创建了一个自定义节处理程序来从app config文件中读取信息,但当我尝试运行它时,会出现以下错误:Object reference未设置为对象的实例 我两天来一直在努力解决这个问题,但运气不好 下面是我的代码:App-config <?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- Configuration section-handler declaration area. --

因此,我创建了一个自定义节处理程序来从app config文件中读取信息,但当我尝试运行它时,会出现以下错误:Object reference未设置为对象的实例

我两天来一直在努力解决这个问题,但运气不好

下面是我的代码:App-config

<?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>
并调用该对象:

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

由于节路径错误,您得到的是空值,它应该是:

ClaimHeaderSection config =
(ClaimHeaderSection)System.Configuration.ConfigurationManager.GetSection(
"propertyValuesGroup/propertyValues");
但是现在,由于您已经设计了自定义配置部分,如果您放置了该有效路径,您将得到一个ConfigurationErrorsException,并显示消息“UnrecognedElement'property.”

根据发布的配置,您需要ClaimHeaderElement的集合,但您不是在自定义部分中定义,而是在定义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的静态方法,这将更容易获取配置部分:

ClaimHeaderSection config = ClaimHeaderSection.GetConfiguration();
在您的配置中,您应该将配置元素的名称从claimHeader更改为add,如果您希望xml节点被称为claimHeader而不是add,则应该将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>


您没有提到编程语言。它看起来像C。假设是这样,我建议您删除一个现有的标记,并用C#标记替换它。这将极大地提高您的问题的可视性,并提高快速回答问题的机会。感谢您的回答,因此这是一个明显的错误,不使用configurationElementCollection,我对此仍然一无所知,我尝试实现您的代码,但仍然得到一个nullReferenceException?我只是尝试它对我有效,我编辑了答案,因为我忘了在配置中做一些更改,但这不会引发空引用异常。。。如果你想,我可以为你发布一个链接,让你下载我刚刚做的小测试,它正在工作,让我知道。谢谢,我让它工作,我没有使用你的具体代码在最后,但你把我放在正确的轨道上!谢谢你的帮助!
<!-- 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>