App.config配置文件的读写,我想实现自定义属性的增减(我只实现了写,但读就是报错)
mqmmx 2006-06-19 11:41:44 以下是我定义的类代码,但只要我加入自定义属性读明就会错,因为没有为这个属性加入定义好的类,但怎么得到已知的文件中自定义属性的名称和值呢
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
namespace ReadAndWriteConfig
{
// Define a custom section.
// The CustomSection type alows to define a custom section
// programmatically.
public sealed class ConfigDataClass :
ConfigurationSection
{
#region 实例化Property的各属性struct
[Serializable]
public struct structProperty
{
private Type _type;
private string _value;
private ConfigurationPropertyOptions _cpo;
public structProperty(Type type,
string defalutValue,
ConfigurationPropertyOptions confPropOption)
{
_type = type;
_value = defalutValue;
_cpo = confPropOption;
}
public structProperty(Type type, string defalutValue)
:
this(type, defalutValue, ConfigurationPropertyOptions.None)
{ }
public structProperty(string defalutValue)
:
this(typeof(string), defalutValue, ConfigurationPropertyOptions.None)
{ }
public Type PropertyType
{
get { return _type; }
}
public string PropertyValue
{
get { return _value; }
}
public ConfigurationPropertyOptions ConfPropOpt
{
get { return _cpo; }
}
}
#endregion
// ConfigurationProperty's fullname
private const string sProperty = "System.Configuration.ConfigurationProperty";
// The collection (property bag) that conatains
// the section properties.
private ConfigurationPropertyCollection _Properties;
//必须静态否则自定义属性无法保存
private static Hashtable _PropertyCollect = new Hashtable();
// The MasUsers property.
private ConfigurationProperty _MaxUsers =
new ConfigurationProperty("maxUsers",
typeof(long), (long)1000,
ConfigurationPropertyOptions.None);
// The MaxIdleTime property.
private ConfigurationProperty _MaxIdleTime =
new ConfigurationProperty("maxIdleTime",
typeof(TimeSpan), TimeSpan.FromMinutes(5),
ConfigurationPropertyOptions.IsRequired);
// CustomSection constructor.
public ConfigDataClass()
{
// Property initialization
_Properties =
new ConfigurationPropertyCollection();
PropertyToCollect();
}
public void AddProperty(string PropertyName, string PropertyValue)
{
AddProperty(PropertyName, new structProperty(PropertyValue));
}
public void AddProperty(string PropertyName, string PropertyValue,
Type PropertyType)
{
AddProperty(PropertyName, new structProperty(PropertyType, PropertyValue));
}
public void AddProperty(string PropertyName, string PropertyValue,
Type PropertyType,
ConfigurationPropertyOptions ptc)
{
AddProperty(PropertyName, new structProperty(PropertyType, PropertyValue, ptc));
}
public void AddProperty(string PropertyName, structProperty PropertyValue)
{
if (_PropertyCollect.ContainsKey(PropertyName) == true)
{
_PropertyCollect.Remove(PropertyName);
}
_PropertyCollect.Add(PropertyName, PropertyValue);
}
/// <summary>
/// 将属性加入属性集合
/// </summary>
private void PropertyToCollect()
{
ConfigurationProperty p;
structProperty sp;
_Properties.Clear();
foreach (DictionaryEntry d in _PropertyCollect)
{
sp = (structProperty)d.Value;
object[] o = new object[4];
o[0] = d.Key.ToString();
o[1] = sp.PropertyType;
o[2] = sp.PropertyValue;
o[3] = sp.ConfPropOpt;
p = (ConfigurationProperty)Assembly.Load(Assembly.GetAssembly(typeof(ConfigurationProperty)).FullName).CreateInstance(sProperty,
false,
BindingFlags.Default,
null,
o,
null,
null);
_Properties.Add(p);
}
_Properties.Add(_MaxIdleTime);
_Properties.Add(_MaxUsers);
}
// This is a key customization.
// It returns the initialized property bag.
protected override ConfigurationPropertyCollection Properties
{
get
{
return _Properties;
}
}
// Customizes the use of CustomSection
// by setting _ReadOnly to false.
// Remember you must use it along with ThrowIfReadOnly.
protected override object GetRuntimeObject()
{
return base.GetRuntimeObject();
}
[LongValidator(MinValue = 1, MaxValue = 1000000,
ExcludeRange = false)]
public long MaxUsers
{
get
{
return (long)this["maxUsers"];
}
set
{
this["maxUsers"] = value;
}
}
[TimeSpanValidator(MinValueString = "0:0:30",
MaxValueString = "5:00:0",
ExcludeRange = false)]
public TimeSpan MaxIdleTime
{
get
{
return (TimeSpan)this["maxIdleTime"];
}
set
{
this["maxIdleTime"] = value;
}
}
}
}