/****************************************************************************** Copyright 2005-2007 R2@DevFx.NET DevFx.NET is free software; you can redistribute it and/or modify it under the terms of the Lesser GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. DevFx.NET is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public License for more details. You should have received a copy of the Lesser GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA /*******************************************************************************/ using System; using System.Collections.Specialized; using System.Configuration; using System.Reflection; using System.Xml; using Lskj.DevFx.Data; namespace Lskj.DevFx.Config.DotNetConfig { /// /// 配置元素基础类,从派生 /// /// /// 此类对基类做了些修改,允许未定义的属性存在 /// 派生类如果要允许未定义的属性,则必须重写 /// public abstract class BaseConfigurationElement : ConfigurationElement { /// /// 获取一个值,该值指示反序列化过程中是否遇到未知属性 /// /// 无法识别的属性的名称 /// 无法识别的属性的值 /// 如果反序列化过程中遇到未知属性,则为true protected override bool OnDeserializeUnrecognizedAttribute(string name, string value) { this.unkownProperties.Add(name, value); return this.OnDeserializeUnrecognizedFlag; } /// /// 获取一个值,该值指示反序列化过程中是否遇到未知元素 /// /// 未知的子元素的名称 /// 用于反序列化的 对象 /// 如果反序列化过程中遇到未知元素,则为 true protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader) { return this.OnDeserializeUnrecognizedFlag; } /// /// 是否遇到未知的属性或元素 /// /// /// 派生类如果要允许未定义的属性,则必须重写本属性 /// protected virtual bool OnDeserializeUnrecognizedFlag { get { return false; } } /// /// 本元素对应的Xml /// public virtual string OuterXml { get { return this.outerXml; } } private string outerXml; private NameValueCollection unkownProperties = new NameValueCollection(); /// /// 读取配置文件中的 XML /// /// 在配置文件中进行读取操作的 /// true,则只序列化集合的键属性;否则为 false protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey) { FieldInfo field = reader.GetType().GetField("_rawXml", FieldMemberInfo.FieldBindingFlags); this.outerXml = (string)field.GetValue(reader); base.DeserializeElement(reader, serializeCollectionKey); } /// /// 获取未定义的属性值 /// /// 属性名 /// 属性值 public virtual string GetPropertyValue(string propertyName) { return this.unkownProperties[propertyName]; } /// /// 获取未定义的属性值 /// /// 属性值类型 /// 属性名 /// 属性值 public virtual T GetPropertyValue(string propertyName) { return this.GetPropertyValue(propertyName, default(T)); } /// /// 获取未定义的属性值 /// /// 属性值类型 /// 属性名 /// 如果此属性不存在需提供的缺省值 /// 属性值 public virtual T GetPropertyValue(string propertyName, T defaultValue) { string propertyValue = this.GetPropertyValue(propertyName); T returnValue = defaultValue; if (!string.IsNullOrEmpty(propertyValue)) { returnValue = (T)Convert.ChangeType(propertyValue, typeof(T)); } return returnValue; } /// /// 检测指定的属性是否存在 /// /// 属性名 /// 是否存在 public virtual bool CheckPropertyExists(string propertyName) { return this.unkownProperties[propertyName] != null; } } }