📄 configelementcollection.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace APLib.Common.Configuration
{
/// <summary>
/// 配置节集合类
/// </summary>
public class ConfigElementCollection : ConfigurationElementCollection
{
/// <summary>
/// 配置节集合类型,AddRemoveClearMap
/// </summary>
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.AddRemoveClearMap;
}
}
/// <summary>
/// 获取子配置节键值
/// </summary>
/// <param name="element">子配置节</param>
/// <returns>子配置节的键值</returns>
protected override object GetElementKey(ConfigurationElement element)
{
return (element as ConfigElement).Name;
}
/// <summary>
/// 创建配置节
/// </summary>
/// <returns>新创建的配置节</returns>
protected override ConfigurationElement CreateNewElement()
{
return new ConfigElement();
}
/// <summary>
/// 创建配置节
/// </summary>
/// <param name="elementName">配置节键值</param>
/// <returns>新创建的配置节</returns>
protected override ConfigurationElement CreateNewElement(string elementName)
{
return new ConfigElement(elementName);
}
/// <summary>
/// 重载IsReadOnly,写操作允许
/// </summary>
/// <returns>false</returns>
public override bool IsReadOnly()
{
return false;
}
/// <summary>
/// 重载索引,获取子配置节
/// </summary>
/// <param name="index">索引号</param>
/// <returns>子配置节</returns>
public ConfigElement this[int index]
{
get
{
return BaseGet(index) as ConfigElement;
}
set
{
if (BaseGet(index) != null)
BaseRemoveAt(index);
BaseAdd(index, value);
}
}
/// <summary>
/// 重载索引,获取子配置节
/// </summary>
/// <param name="name">子配置节键值</param>
/// <returns>子配置节</returns>
public new ConfigElement this[string name]
{
get
{
return BaseGet(name) as ConfigElement;
}
set
{
if (BaseGet(name) != null)
BaseRemove(name);
BaseAdd(value);
}
}
/// <summary>
/// 添加子配置节
/// </summary>
/// <param name="name">新子配置节键值</param>
/// <param name="value">新子配置节配置值</param>
public virtual void Add(string name, string value)
{
ConfigElement element = (ConfigElement)CreateNewElement(name);
this[name] = element;
element.Value = value;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -