自定义配置节处理程序的问题

时间:2013-04-25 18:43:01

标签: c# app-config configurationsection custom-sections

所以我对使用app-config文件全新,我试图创建一个自定义配置处理程序,以便我可以从同一个键读取多个值,我按照Microsoft网站上的文档但我遇到了问题。

每次我尝试运行我的代码时都会抛出此错误

“无法识别的属性'数据类型'。请注意,属性名称区分大小写。(C:\ Users \ stephen.carmody \ Desktop \ FlatFileFactory - Copy \ FlatFileFactory \ bin \ Debug \ FlatFileFactory.vshost.exe.Config line 21 )“

它似乎只识别元素中的前两个值,第三个“数据类型”抛出错误

以下是我的配置文件

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

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


<propertyValuesGroup>
<propertyValues>
  <cHeaderProperty name="txnNo" nullable="yes" datatype="int" maxlength="" />
</propertyValues>
</propertyValuesGroup>

</configuration>

以下是我的自定义处理程序类:

namespace FlatFileTestCaseAutomater
{
    class CustomConfigurationSectionHandler : ConfigurationSection
    {
        [ConfigurationProperty("cHeaderProperty")]
        public CHeaderPropertyElement Property
    {
        get
        {
            return (CHeaderPropertyElement)this["cHeaderProperty"];
        }
        set
        { this["cHeaderProperty"] = value; }
    }
}


public class ClaimHeaderElement : ConfigurationElement
{
    [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public String Name
    {
        get
        {
            return (String)this["name"];
        }
        set
        {
            this["name"] = value;
        }
    }

    [ConfigurationProperty("dataType", DefaultValue = "", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public String DataType
    {
        get
        {
            return (String)this["dataType"];
        }
        set
        {
            this["dataType"] = value;
        }
    }

    [ConfigurationProperty("maxLength", DefaultValue = int.MaxValue, IsRequired = false)]
   // [IntegerValidator(ExcludeRange = false, MaxValue = 24, MinValue = 0)]
    public int MaxLength
    {
        get
        { return (int)this["maxLength"]; }
        set
        { this["maxLength"] = value; }
    }

}

}

以下是调试期间发生中断的代码片段:

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

我知道之前发过了类似的帖子,但我已经在这里待了好几个小时而没有运气

1 个答案:

答案 0 :(得分:0)

  

我可能错了,但配置中的名字似乎不一样   'case',它们都是小写的(数据类型和maxlength),错误的时候   粘贴在这里?

属性确实区分大小写 - 它应该是(基于您的代码)

<cHeaderProperty name="txnNo" nullable="yes" dataType="int" maxLength="" />