c#自定义配置节点(web.config、app.config)

这里提供一个实例的完整写法,深入了解的话请参考官方的文档

1.项目引用System.Configuration

2.编写PositionConfigurationSection,代码如下:

public class PositionConfigurationSection: ConfigurationSection
    {
        [ConfigurationProperty("Positions", IsRequired =true)]
        [ConfigurationCollection(typeof(PositionSection),CollectionType =ConfigurationElementCollectionType.AddRemoveClearMap,AddItemName ="Position")]
        public PositionContainer Positions
        {
            get => base["Positions"] as PositionContainer;
            set => base["Positions"] = value;
        }
    }

3.编写PositionContainer:

 public class PositionContainer : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new PositionSection();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as PositionSection).ID;
        }
    }

4.编写PositionSection:

  public class PositionSection: ConfigurationElement
    {
        [ConfigurationProperty("ID", IsRequired = true)]
        public int ID
        {
            get =>Convert.ToInt32(base["ID"]);
            set => base["ID"] = value;
        }
        [ConfigurationProperty("Title", IsRequired = true)]
        public string Title
        {
            get => base["Title"].ToString();
            set => base["Title"] = value;
        }
        [ConfigurationProperty("Blocks", IsRequired = true)]
        [ConfigurationCollection(typeof(BlockSection), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap,AddItemName ="Block")]
        public BlockContainer Blocks
        {
            get => base["Blocks"] as BlockContainer;
            set => base["Blocks"] = value;
        }
    }

5.编写BlockContainer:

 public class BlockContainer : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new BlockSection();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as BlockSection).ID;
        }
    }

6.编写BlockSection:

 public class BlockSection: ConfigurationElement
    {
        [ConfigurationProperty("ID", IsRequired = true)]
        public int ID
        {
            get => Convert.ToInt32(base["ID"]);
            set => base["ID"] = value;
        }
        [ConfigurationProperty("Title", IsRequired = true)]
        public string Title
        {
            get => base["Title"].ToString();
            set => base["Title"] = value;
        }
        [ConfigurationProperty("X", IsRequired = true)]
        public double X
        {
            get => Convert.ToDouble(base["X"]);
            set => base["X"] = value;
        }
        [ConfigurationProperty("Y", IsRequired = true)]
        public double Y
        {
            get => Convert.ToDouble(base["Y"]);
            set => base["Y"] = value;
        }
        [ConfigurationProperty("Width", IsRequired = true)]
        public double Width
        {
            get => Convert.ToDouble(base["Width"]);
            set => base["Width"] = value;
        }
        [ConfigurationProperty("Height", IsRequired = true)]
        public double Height
        {
            get => Convert.ToDouble(base["Height"]);
            set => base["Height"] = value;
        }
    }

7.在App.Config(本实例是一个Wpf项目)代码:



 
   


 
 
   
     
       
         
         
       

     

     
       
         
         
       

     

   

 

 
   
 

8.在App.Xaml.cs里面写测试代码:

  protected override void OnStartup(StartupEventArgs e)
        {
            PositionConfigurationSection section = ConfigurationManager.GetSection("PositionConfig") as PositionConfigurationSection;
            foreach (PositionSection position in section.Positions)
            {
                foreach (BlockSection block in position.Blocks)
                {

                }
            }
            base.OnStartup(e);
        }

总结:该实例演示的是通过配置文件来决定系统有哪些席位(PositionSection),席位里面包含哪些区块(BlockSection),以及配置了每个区块的大小和位置。这个实例演示的内容基本上能够满足我们大部分情况下的使用要求了。值得注意的是这句代码:

[ConfigurationCollection(typeof(BlockSection), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap,AddItemName ="Block")]

这句AddItemName ="Block",如果不写,系统默认的是add,这里面为了让xmal可读性更高些,把添加item的名字改掉了。

这一句里面的WpfApp25是我的测试项目名称。

你可能感兴趣的:(c#自定义配置节点(web.config、app.config))