我正在尝试在 app.config 文件中创建自定义部分
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="BlogSettings" type="ConsoleApplication1.BlogSettings,
ConsoleApplication1" />
</configSections>
<BlogSettings
Price="10"
title="BLACKswastik" />
</configuration>
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string title = BlogSettings.Settings.Title;
Console.WriteLine(title);
Console.ReadKey();
}
}
public class BlogSettings : ConfigurationSection
{
private static BlogSettings settings
= ConfigurationManager.GetSection("BlogSettings") as BlogSettings;
public static BlogSettings Settings
{
get
{
return settings;
}
}
[ConfigurationProperty("Price"
, DefaultValue = 20
, IsRequired = false)]
[IntegerValidator(MinValue = 1
, MaxValue = 100)]
public int Price
{
get { return (int)this["Price"]; }
set { this["Price"] = value; }
}
[ConfigurationProperty("title"
, IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\\"
, MinLength = 1
, MaxLength = 256)]
public string Title
{
get { return (string)this["title"]; }
set { this["title"] = value; }
}
}
}
但是当我运行这段代码时,我收到了这个错误:
'ConsoleApplication1.BlogSettings' 的类型初始化程序引发了异常。
请告诉我我在做什么错。