所以我制作了一个自定义部分处理程序来从 app-config 文件中读取信息,但是当我尝试运行它时,我收到以下错误:对象引用未设置为对象的实例。
我已经尝试解决这个问题两天了,但没有运气
这是我的代码: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/");