Create custom section in config file

大兔子大兔子 提交于 2019-12-08 04:24:02

问题


I am trying to create custom section in app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="BlogSettings" type="ConsoleApplication1.BlogSettings,   
      ConsoleApplication1" />
  </configSections>
  <BlogSettings
    Price="10"
    title="BLACKswastik" />
</configuration> 

C# code :

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; }
        }
    }
}

but when I run this code I am getting this error:

The type initializer for 'ConsoleApplication1.BlogSettings' threw an exception.

Please suggest me whats wrong I am doing.


回答1:


You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

  • Unraveling the mysteries of .NET 2.0 configuration
  • Decoding the mysteries of .NET 2.0 configuration
  • Cracking the mysteries of .NET 2.0 configuration

Highly recommended, well written and extremely helpful! This will give you a thorough understanding of the .NET configuration system.

Phil Haack also has a great blog post Three easy steps to a custom configuration section that will give you a quick head-start into building your own custom config sections.

To design your own custom section, there's also a handy tool (Visual Studio add-in) called Configuration Section Designer that will make it very easy and simple to create your own custom section and have it build up all the necessary code to handle that custom section.




回答2:


move this out of the config section

 <BlogSettings
    Price="10"
    title="BLACKswastik" />

You made a new config reference so it can be its own node now.



来源:https://stackoverflow.com/questions/13226711/create-custom-section-in-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!