下面是我粘貼下面的代碼時遇到的錯誤。如何在c#中實現ConfigurationElement?
無法創建類ZDRCreatorTests.ZDRCreatorTests的實例。錯誤:System.Configuration.ConfigurationErrorsException:無法分析屬性'indexedFolder'的默認值。錯誤是:無法找到支持從字符串類型的屬性「indexedFolder「DirectoryInfo的」轉換/ A轉換器..
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後將它們轉換爲其他對象?或者,是否有一個地方可以繼續,並將它們構建到將在內部使用的對象中。輸入驗證在哪裏發生?
你說的一切都是真的。 CSD是一個了不起的工具,但幾乎是最近完整版的Visual Studio。很傷心看到它衰弱。 – QueueHammer