首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >App-config,自定义节处理程序对象返回为空

App-config,自定义节处理程序对象返回为空
EN

Stack Overflow用户
提问于 2013-05-01 03:12:46
回答 1查看 3.3K关注 0票数 0

因此,我创建了一个自定义的节处理程序来从app-config文件中读取信息,但是当我尝试运行它时,我得到了以下错误: object reference not set to a Object of a instance。

我已经试着解决这个问题两天了,但是没有成功。

下面是我的代码: App - config

代码语言:javascript
运行
复制
<?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>

我的定制节处理程序:

代码语言:javascript
运行
复制
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; }
        }

    }

}

并调用该对象:

代码语言:javascript
运行
复制
 FlatFileTestCaseAutomater.ClaimHeaderSection config =
    (FlatFileTestCaseAutomater.ClaimHeaderSection)System.Configuration.ConfigurationManager.GetSection(
    "propertyValuesGroup/propertyValues/property/");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-01 07:55:09

由于节路径错误,您将得到一个null,它应该是:

代码语言:javascript
运行
复制
ClaimHeaderSection config =
(ClaimHeaderSection)System.Configuration.ConfigurationManager.GetSection(
"propertyValuesGroup/propertyValues");

但是现在,由于您已经设计了自定义配置节,如果您放入该有效路径,您将得到一个带有消息"Unrecognized element‘ConfigurationErrorsException’“的属性。

根据您发布的配置,您需要一个ClaimHeaderElement集合,但是您并没有在自定义部分中定义ConfigurationElementCollection,而是定义了一个ConfigurationElement。

为了工作,你应该实现一个ConfigurationElementCollection,如下所示:

代码语言:javascript
运行
复制
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;
        }
    }
}

现在,在您的自定义部分中,您必须添加集合:

代码语言:javascript
运行
复制
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的静态方法,这将使获取配置节变得更容易:

代码语言:javascript
运行
复制
ClaimHeaderSection config = ClaimHeaderSection.GetConfiguration();

在您的配置中,您应该将配置元素的名称从claimHeader更改为add,如果您希望xml节点名为claimHeader而不是add,则应该将ConfigurationCollection属性中的AddItemName值从add更改为claimHeader。我会把它留在" add“中,因为我把add放在了属性中:

代码语言:javascript
运行
复制
<!-- 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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16307297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档