【问题标题】:How to Implement ConfigurationElement in c#?如何在 C# 中实现 ConfigurationElement?
【发布时间】:2011-01-17 04:45:16
【问题描述】:

这是我在粘贴下面的代码时遇到的错误。

无法创建类 ZDRCreatorTests.ZDRCreatorTests 的实例。错误:System.Configuration.ConfigurationErrorsException:无法解析属性“indexedFolder”的默认值。错误是:找不到支持“DirectoryInfo”类型的属性“indexedFolder”的字符串转换为/从字符串转换的转换器..

namespace ZDRCreator
{
    public class ZDRCreatorElement : ConfigurationElement
    {
        // Create the element.
        public ZDRCreatorElement()
        { }

        // Get or set the IndexedFolder
        [ConfigurationProperty("indexedFolder", DefaultValue = "", IsRequired = true)]
        public DirectoryInfo IndexedFolder {
            get { return (DirectoryInfo)this["indexedFolder"]; }
            set { this["indexedFolder"] = value; }
        }

        // Get or set the OutputFolder
        [ConfigurationProperty("outputFolder", DefaultValue = "", IsRequired = true)]
        public DirectoryInfo OutputFolder {
            get { return (DirectoryInfo)this["outputFolder"]; }
            set { this["outputFolder"] = value; }
        }

        // Get or set the ZDRFile 
        [ConfigurationProperty("ZDRFile", DefaultValue = "", IsRequired = true)]
        public FileInfo ZDRFile {
            get { return (FileInfo)this["ZDRFile"]; }
            set { this["ZDRFile"] = value; }
        }

        // Get or set the overwriteOutput flag
        [ConfigurationProperty("overwriteOutput", DefaultValue = "false", IsRequired = true)]
        public bool OverwriteOutput {
            get { return (bool)this["overwriteOutput"]; }
            set { this["overwriteOutput"] = value; }
        }

        // Get or set the OutputFile
        [ConfigurationProperty("outputFile", DefaultValue = "", IsRequired = true)]
        public String OutputFile {
            get { return (String)this["outputFile"]; }
            set { this["outputFile"] = value; }
        }
        // Get or set the OutputFile
        [ConfigurationProperty("pathMask", DefaultValue = "", IsRequired = true)]
        public String PathMask {
            get { return (String)this["pathMask"]; }
            set { this["pathMask"] = value; }
        }
    }
}

我意识到错误是因为我试图将字符串放入 DirectoryInfo 对象中。我的问题是:我想只存储从 xml 读取的字符串或原始数据类型,然后在读取 xml 后将它们转换为其他对象吗?或者,是否有一个地方我可以继续并将它们构造成将在内部使用的对象。输入验证将在哪里进行?

【问题讨论】:

    标签: c# xml configuration


    【解决方案1】:

    我知道这并不能直接回答您的问题,但我强烈建议您查看Configuration Section Designer project on CodePlex。

    它将为您提供应用程序中配置部分的设计时体验,从设计器为您生成类代码,以及用于将它们放入配置文件的模板。

    必须自己手动完成所有这些工作非常、非常乏味,而且我还没有看到配置部分设计器无法处理的情况。 p>

    【讨论】:

    • 你所说的一切都是真的。 CSD 是一个了不起的工具,但近来几乎是 Visual Studio 的完整版本。很遗憾看到它萎靡不振。
    【解决方案2】:

    您可以使用转换器添加TypeConventerAttribute,该转换器将字符串(来自配置)从/到 DirectoryInfo。转换器是从TypeConverter 派生的类。

    [ConfigurationProperty("ZDRFile", DefaultValue = "", IsRequired = true)]
    [TypeConverter(typeof(YourCustomFileInfoTypeConverter))]
    public FileInfo ZDRFile {
        get { return (FileInfo)this["ZDRFile"]; }
        set { this["ZDRFile"] = value; }
    }
    

    【讨论】:

    猜你喜欢
    • 2010-09-30
    • 1970-01-01
    • 2021-08-18
    • 2010-09-28
    • 2016-04-13
    • 2014-10-29
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多