开发者

How to Implement ConfigurationElement in c#?

Here's the error I'm getting with the code pasted below.

Unable to create instance of class ZDRCreatorTests.ZDRCreatorTests. Error: System.Configuration.ConfigurationErrorsException: The default value of the property 'indexedFolder' cannot be parsed. The error is: Unable to find a converter that supports conversion to/from string for the property 'indexedFolder' of type 'DirectoryInfo'..

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; }
        }
    }
}

I realize the error is because I'm trying to put a string in a DirectoryInfo object. My question is this: Am I suppose to only store strings or primitive data types read from the xml, then convert them to other objects once the xml is read? OR, is there a place where I can go ahead and c开发者_如何学运维onstruct them into the object that will be used internally. Where will validation of the input occur?


You can add TypeConventerAttribute with converter that will convert string(that will came from config) from/to the DirectoryInfo. Converter is class derived from TypeConverter.

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


I know this doesn't directly answer your question, but I strongly encourage you to take a look at the Configuration Section Designer project on CodePlex.

It will give you a design-time experience for configuration sections in your application, generating the class code for you from the designer, as well as templates for putting them in your config file as well.

Having to do all of this on your own, by hand, is very, very tedious, and I haven't seen a situation yet that the Configuration Section Designer doesn't handle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜