因此,我创建了一个自定义的节处理程序来从app-config文件中读取信息,但是当我尝试运行它时,我得到了以下错误: object reference not set to a Object of a instance。
我已经试着解决这个问题两天了,但是没有成功。
下面是我的代码: 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>我的定制节处理程序:
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/");发布于 2013-05-01 07:55:09
由于节路径错误,您将得到一个null,它应该是:
ClaimHeaderSection config =
(ClaimHeaderSection)System.Configuration.ConfigurationManager.GetSection(
"propertyValuesGroup/propertyValues");但是现在,由于您已经设计了自定义配置节,如果您放入该有效路径,您将得到一个带有消息"Unrecognized element‘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的静态方法,这将使获取配置节变得更容易:
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>https://stackoverflow.com/questions/16307297
复制相似问题