下面是我用下面粘贴的代码得到的错误。
无法创建类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后将其转换为其他对象?或者,有没有一个地方,我可以继续并将它们构造到内部使用的对象中。在哪里对输入进行验证?
发布于 2010-02-16 07:22:08
我知道这不能直接回答你的问题,但我强烈建议你看看Configuration Section Designer project on CodePlex。
它将为您提供应用程序中配置节的设计时体验,从设计器中为您生成类代码,以及将它们放入配置文件中的模板。
必须自己手工完成所有这些工作是非常、非常乏味的,而且我还没有看到配置节设计器不能处理的情况。
发布于 2012-02-09 05:39:48
您可以添加带有转换器的TypeConventerAttribute,该转换器将字符串(来自配置)从/转换为DirectoryInfo。Converter是从TypeConverter派生的类。
[ConfigurationProperty("ZDRFile", DefaultValue = "", IsRequired = true)]
[TypeConverter(typeof(YourCustomFileInfoTypeConverter))]
public FileInfo ZDRFile {
get { return (FileInfo)this["ZDRFile"]; }
set { this["ZDRFile"] = value; }
}https://stackoverflow.com/questions/2269629
复制相似问题