我正在编写从app.config读取自定义配置部分的代码,但是我收到了一些错误,希望任何人都可以帮忙搞清楚。
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="Psptune"
type="ConfigurationSettings.PsptuneSection, ConfigurationSettings"/>
</configSections>
<Psptune>
<psp>
<FolderLocataions>
<add name="PspFolder" directory="aa"/>
<add name="IsoFolder" directory="aa"/>
</FolderLocataions>
</psp>
</Psptune>
<startup>
<supportedRuntime version="v4.0"
sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
using System;
namespace ConfigurationSettings
{
class Program
{
static void Main(string[] args)
{
try
{
var sec = PsptuneSection.Settings;
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
Console.ReadKey();
}
}
}
using System.Configuration;
namespace ConfigurationSettings
{
public class FolderLocationElement : ConfigurationElement
{
[ConfigurationProperty( "name", DefaultValue = "default", IsRequired = false)]
[StringValidator(MinLength = 5, MaxLength = 256)]
public string Name
{
get { return (string) this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("directory", DefaultValue = "default", IsRequired = false)]
[StringValidator(MinLength = 5, MaxLength = 256)]
public string Directory
{
get { return (string)this["directory"]; }
set { this["directory"] = value; }
}
}
}
using System.Configuration;
namespace ConfigurationSettings
{
public class FolderLocationElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FolderLocationElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((FolderLocationElement) element).Name;
}
public FolderLocationElement this[int index]
{
get { return (FolderLocationElement) BaseGet(index); }
}
public void Add(FolderLocationElement element)
{
BaseAdd(element);
}
}
}
using System.Configuration;
namespace ConfigurationSettings
{
public class PspElement : ConfigurationElement
{
[ConfigurationProperty("FolderLocataions", DefaultValue = "default", IsRequired = false)]
[StringValidator(MinLength = 5, MaxLength = 256)]
public FolderLocationElementCollection FolderLocataions
{
get { return (FolderLocationElementCollection) this["FolderLocataions"]; }
set { this["FolderLocataions"] = value; }
}
}
}
using System.Configuration;
namespace ConfigurationSettings
{
public class PsptuneSection : ConfigurationSection
{
private static PsptuneSection _settings =
ConfigurationManager.GetSection("Psptune") as PsptuneSection;
public static PsptuneSection Settings { get { return _settings; } }
[ConfigurationProperty("psp", DefaultValue = "default", IsRequired = false)]
public PspElement Psp
{
get { return (PspElement) this["psp"]; }
set { this["psp"] = value; }
}
}
}
我得到的例外是:
System.TypeInitializationException: The type initializer for 'ConfigurationSetti
ngs.PsptuneSection' threw an exception. ---> System.Configuration.ConfigurationE
rrorsException: The default value of the property 'psp' cannot be parsed. The er
ror is: Object reference not set to an instance of an object.
答案 0 :(得分:0)
没关系我的朋友我弄明白了,你们可以停下来投票让我失望。问题是某些元素不能具有默认值