首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在c#中实现ConfigurationElement?

如何在c#中实现ConfigurationElement?
EN

Stack Overflow用户
提问于 2010-02-16 07:14:54
回答 2查看 5K关注 0票数 0

下面是我用下面粘贴的代码得到的错误。

无法创建类ZDRCreatorTests.ZDRCreatorTests的实例。错误: System.Configuration.ConfigurationErrorsException:无法分析属性'indexedFolder‘的默认值。错误是:找不到支持与类型为‘DirectoryInfo’的属性'indexedFolder‘相互转换的字符串的转换器。

代码语言:javascript
复制
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后将其转换为其他对象?或者,有没有一个地方,我可以继续并将它们构造到内部使用的对象中。在哪里对输入进行验证?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-16 07:22:08

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

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

必须自己手工完成所有这些工作是非常、非常乏味的,而且我还没有看到配置节设计器不能处理的情况。

票数 1
EN

Stack Overflow用户

发布于 2012-02-09 05:39:48

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

代码语言:javascript
复制
[ConfigurationProperty("ZDRFile", DefaultValue = "", IsRequired = true)]
[TypeConverter(typeof(YourCustomFileInfoTypeConverter))]
public FileInfo ZDRFile {
    get { return (FileInfo)this["ZDRFile"]; }
    set { this["ZDRFile"] = value; }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2269629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档