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

本文关键字:问题 程序 处理 配置 自定义 | 更新日期: 2023-09-27 18:10:59

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

每次我试着运行我的代码,它抛出这个错误

"无法识别的属性'datatype'。注意,属性名是区分大小写的。用户(C: ' '斯蒂芬。carmody'Desktop'FlatFileFactory - Copy'FlatFileFactory'bin'Debug'FlatFileFactory.vshos .exe配置第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");

我知道一个类似的帖子之前已经发布,但我已经在这个几个小时了,现在没有运气

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

我可能是错的,但你的名字在配置似乎不一样'case',它们都是小写的(datatype和maxlength),当粘贴在这里吗?

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

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