如何在c#中实现ConfigurationElement?
下面是我在粘贴代码时遇到的错误 无法创建类ZDRCreateTests.ZDRCreateTests的实例。错误:System.Configuration.ConfigurationErrorsException:无法分析属性“indexedFolder”的默认值。错误是:找不到支持与“DirectoryInfo”类型的属性“indexedFolder”的字符串进行转换的转换器如何在c#中实现ConfigurationElement?,c#,xml,configuration,C#,Xml,Configuration,下面是我在粘贴代码时遇到的错误 无法创建类ZDRCreateTests.ZDRCreateTests的实例。错误:System.Configuration.ConfigurationErrorsException:无法分析属性“indexedFolder”的默认值。错误是:找不到支持与“DirectoryInfo”类型的属性“indexedFolder”的字符串进行转换的转换器 namespace ZDRCreator { public class ZDRCreatorElement :
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后将它们转换为其他对象?或者,是否有一个地方可以让我继续将它们构建到将在内部使用的对象中。输入的验证将在哪里进行 我知道这并不能直接回答你的问题,但我强烈建议你看一下 它将为您的应用程序中的配置部分提供设计时体验,从设计器为您生成类代码,以及将它们放入配置文件的模板
手动完成所有这些都是非常非常乏味的,而且我还没有看到配置部分设计器无法处理的情况。我知道这并不能直接回答您的问题,但我强烈鼓励您查看 它将为您的应用程序中的配置部分提供设计时体验,从设计器为您生成类代码,以及将它们放入配置文件的模板 必须自己手动完成所有这一切是非常非常乏味的,我还没有看到配置部分设计器无法处理的情况。您可以添加转换器,将字符串(来自配置)从/转换到DirectoryInfo。转换器是从派生的类 您可以添加将字符串(来自配置)从/转换为DirectoryInfo的转换器。转换器是从派生的类
我更喜欢你的答案。对于那些后来偶然发现这一点的人,这里有一篇MSDN文章指导您创建自定义类型转换器:我更喜欢您的答案。对于那些后来偶然发现这一点的人,这里有一篇MSDN文章,指导您创建自定义类型转换器:您所说的一切都是完全正确的。CSD是一个令人惊叹的工具,但如今它几乎是VisualStudio的完整版本。你说的一切都是真的。CSD是一个令人惊叹的工具,但如今它几乎是VisualStudio的完整版本。看到它萎靡不振,我很难过。
[ConfigurationProperty("ZDRFile", DefaultValue = "", IsRequired = true)]
[TypeConverter(typeof(YourCustomFileInfoTypeConverter))]
public FileInfo ZDRFile {
get { return (FileInfo)this["ZDRFile"]; }
set { this["ZDRFile"] = value; }
}