/// <summary>
/// 配置文件,用来保存对象的Field和Propertie为XML文件
/// 保存类型的Object必须为有IsIObjConfigProxy属性。
/// 只保存public 的Field 和有get和set的Propertie,如果存在[NonSaveConfig]或[Browsable(false)]属性则不保存,如果存在[NeedSaveConfig]属性则保存
/// by:Gene
/// </summary>
public class ObjConfig : IConfigParamesProxy
{
object tag;
public ObjConfig(object obj)
{
tag = obj;
configPath = System.Windows.Forms.Application.StartupPath + "/config/" + tag.GetType().Name + ".xml";
}
private string configPath = "";
public virtual string ConfigFile
{
get
{
return configPath;
}
set
{
this.configPath = value;
}
}
public virtual bool Load()
{
if (!System.IO.File.Exists(ConfigFile)) return false;
MyXmlConfigFile configFile = new MyXmlConfigFile(ConfigFile);
try
{
ConfigParames = configFile.GetItemValue(tag.GetType().Name, "ConfigParames") as Dictionary<string, object>;
return true;
}
catch { return false; }
}
public virtual bool Load(string xml)
{
MyXmlConfigFile configFile = new MyXmlConfigFile();
configFile.LoadXml(xml);
try
{
ConfigParames = configFile.GetItemValue(tag.GetType().Name, "ConfigParames") as Dictionary<string, object>;
return true;
}
catch { return false; }
}
public virtual void Save()
{
MyXmlConfigFile configFile = new MyXmlConfigFile();
configFile.AddComment("配置文件。 Author: EuGene");
configFile.AddComment("!!!配置文件自动生成,请勿随便更改。!!!");
XmlElement con = configFile.AddConfig(tag.GetType().Name);
configFile.AddItem(con, "ConfigParames", ConfigParames);
configFile.Save(ConfigFile);
}
public virtual string SaveToXmlStr()
{
MyXmlConfigFile configFile = new MyXmlConfigFile();
configFile.AddComment("配置文件。 Author: EuGene");
configFile.AddComment("!!!配置文件自动生成,请勿随便更改。!!!");
XmlElement con = configFile.AddConfig(tag.GetType().Name);
configFile.AddItem(con, "ConfigParames", ConfigParames);
return configFile.SaveToXmlStr();
}
[NonSaveConfig]
public virtual Dictionary<string, object> ConfigParames
{
get
{
if (tag is IConfigParamesProxy)
{
return ((IConfigParamesProxy)tag).ConfigParames;
}
Dictionary<string, object> objs = new Dictionary<string, object>();
objs.Add("_ObjectTypeName",tag.GetType().Assembly.FullName+"$"+tag.GetType().ToString());
foreach (FieldInfo fi in tag.GetType().GetFields())
{
if (!IsNeedSave(fi.GetCustomAttributes(true)) || !fi.IsPublic)
{
continue;
}
else
{
object obj = fi.GetValue(tag);
objs.Add(fi.Name, getGetData(obj));
}
}
foreach (PropertyInfo pi in tag.GetType().GetProperties())
{
if (!IsNeedSave(pi.GetCustomAttributes(true)) || !(pi.CanRead && pi.CanWrite))
{
continue;
}
else
{
object obj = pi.GetValue(tag, null);
objs.Add(pi.Name, getGetData(obj));
}
}
return objs;
}
set
{
if (value == null) return;
if (tag is IConfigParamesProxy)
{
((IConfigParamesProxy)tag).ConfigParames = value;
return;
}
foreach (FieldInfo fi in tag.GetType().GetFields())
{
if (!value.ContainsKey(fi.Name) || !fi.IsPublic) continue;
fi.SetValue(tag, getSetData(fi.FieldType, value[fi.Name]));
}
foreach (PropertyInfo pi in tag.GetType().GetProperties())
{
if (!value.ContainsKey(pi.Name) || !(pi.CanRead && pi.CanWrite)) continue;
try
{
pi.SetValue(tag, getSetData(pi.PropertyType, value[pi.Name]), null);
}
catch
{
}
}
}
}
private static object createObj(Type type)
{
object obj = null;
try
{
if (obj == null)
{
obj = System.Activator.CreateInstance(type);
}
}
catch { }
try
{
if (obj == null)
{
obj = Reflection.ClassNew(type.Assembly.FullName, type.FullName, null);
}
}
catch { }
return obj;
}
public static object getSetData(Type type, object obj2)
{
if (MyXmlConfigFile.IsGType(type)) return obj2;
object obj = null;
if (obj2 is Dictionary<string, object> && ((Dictionary<string, object>)obj2).ContainsKey("_ObjectTypeName"))
{
string str = ((Dictionary<string, object>)obj2)["_ObjectTypeName"].ToString();
if (str.Contains("$"))
{
obj = Reflection.ClassNew(str.Split('$')[0], str.Split('$')[1],null);
}
else
{
//System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
//obj = System.Activator.CreateInstance(assembly.FullName,str);
obj = Reflection.ClassNew(str, null);
}
}
else if(type!=null)
{
obj = createObj(type);
}
if (obj == null) return obj;
if (IsIObjConfigProxy(obj))
{
ObjConfig objConfig = new ObjConfig(obj);
objConfig.ConfigParames = obj2 as Dictionary<string, object>;
return obj;
}
else if (obj is IList && obj2 is IList)
{
IList idc = obj as IList;
foreach (object obj1 in (IList)obj2)
{
Type[] types = type.GetGenericArguments();
idc.Add(getSetData(types.Length < 1 ? null : types[0], obj1));
}
return idc;
}
else if (obj is Array && obj2 is Array)
{
Array idc = obj as Array;
for (int i = 0; i < idc.Length; i++)
{
Type[] types = type.GetGenericArguments();
idc.SetValue(getSetData(types.Length < 1 ? null : types[0], ((Array)obj2).GetValue(i)), i);
}
return idc;
}
else if (obj is IDictionary && obj2 is IDictionary)
{
IDictionary idc = obj as IDictionary;
foreach (object obj1 in ((IDictionary)obj2).Keys)
{
if (((IDictionary)obj2)[obj1] != null)
{
idc.Add(obj1.ToString(), getSetData(((IDictionary)obj2)[obj1].GetType(), ((IDictionary)obj2)[obj1]));
}else
idc.Add(obj1.ToString(), getSetData(type.GetGenericArguments()[1], ((IDictionary)obj2)[obj1]));
}
return idc;
}
else
return obj2;
}
public static object getGetData(object obj)
{
if (obj == null) return obj;
if (obj is IConfigParamesProxy)
{
return ((IConfigParamesProxy)obj).ConfigParames;
}
else if (obj is IObjConfigProxy)
{
IObjConfigProxy idc = obj as IObjConfigProxy;
return idc.ObjConfig.ConfigParames;
}
else if (IsIObjConfigProxy(obj))
{
ObjConfig objConfig = new ObjConfig(obj);
return objConfig.ConfigParames;
}
else if (obj is IList)
{
List<object> objs = new List<object>();
foreach (object obj2 in (IList)obj)
{
objs.Add(getGetData(obj2));
}
return objs;
}
else if (obj is Array)
{
object[] objs = new object[((Array)obj).Length];
for (int i = 0; i < objs.Length; i++)
{
objs.SetValue(getGetData(objs.GetValue(i)), i);
}
return objs;
}
else if (obj is IDictionary)
{
Dictionary<string, object> objs = new Dictionary<string, object>();
IDictionary ids = obj as IDictionary;
foreach (object obj2 in ids.Keys)
{
objs.Add(obj2.ToString(), getGetData(ids[obj2]));
}
return objs;
}
else
return obj;
}
private bool IsNeedSave(object[] attrs)
{
foreach (System.Attribute attr in attrs)
{
if (attr is NonSaveConfig)
{
return false;
}
if (attr is NeedSaveConfig)
{
return true;
}
}
foreach (System.Attribute attr in attrs)
{
if (attr is System.ComponentModel.BrowsableAttribute)
{
return ((System.ComponentModel.BrowsableAttribute)attr).Browsable;
}
}
return true;
}
private static bool IsIObjConfigProxy(object obj)
{
if (obj == null) return false;
foreach (object o in obj.GetType().GetCustomAttributes(true))
{
if (o is IsIObjConfigProxy)
{
return true;
}
}
return false;
}
}
#region 属性
[Serializable()]
[System.AttributeUsage(System.AttributeTargets.Class)]
public class IsIObjConfigProxy : System.Attribute
{ }
[Serializable()]
[System.AttributeUsage(System.AttributeTargets.Field |
System.AttributeTargets.Property)]
public class NonSaveConfig : System.Attribute
{ }
[Serializable()]
[System.AttributeUsage(System.AttributeTargets.Field |
System.AttributeTargets.Property)]
public class NeedSaveConfig : System.Attribute
{ }
#endregion
#region XML配置工具
/// <summary>
/// 扩展之前的XmlConfigFile 添加了Color类型
/// by:Chires
/// </summary>
public class XmlConfigFile
{
public delegate object GetItemValueObject(XmlConfigFile configFile, XmlElement item, string typeName);
public delegate XmlElement AddItemObject(XmlConfigFile configFile, XmlElement config, string name, object value);
public XmlConfigFile()
{
doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null));
doc.AppendChild(doc.CreateElement(TagConfigs));
}
public XmlConfigFile(string filePath)
{
this.filePath = filePath;
try
{
doc.Load(filePath);
}
catch(Exception ee)
{
LoggingService.Error(ee.Message,ee);
}
}
public void LoadXml(string xml)
{
try
{
doc.LoadXml(xml);
}
catch
{
}
}
public XmlElement AddConfig(string name)
{
XmlElement config = doc.CreateElement(TagConfig);
AddNameAttribute(config, name);
doc.DocumentElement.AppendChild(config);
return config;
}
public XmlElement AddItem(XmlElement config)
{
XmlElement item = doc.CreateElement(TagItem);
config.AppendChild(item);
return item;
}
public XmlElement AddItem(XmlElement config, string name)
{
XmlElement item = AddItem(config);
if (name != null && name.Length > 0)
{
AddNameAttribute(item, name);
}
return item;
}
public XmlElement AddItem(XmlElement config, string name, Type type)
{
XmlElement item = AddItem(config, name);
AddTypeAttribute(item, type);
return item;
}
public XmlElement AddItem(XmlElement config, string name, string value)
{
XmlElement item = AddItem(config, name, typeof(string));
if (value != null && value.Length > 0)
{
item.AppendChild(doc.CreateTextNode(value));
}
return item;
}
public XmlElement AddItem(XmlElement config, string name, string value, AddItemObject callBack)
{
return AddItem(config, name, value);
}
public XmlElement AddItem(XmlElement config, string name, int value)
{
XmlElement item = AddItem(config, name, typeof(int));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, int value, AddItemObject callBack)
{
return AddItem(config, name, value);
}
public XmlElement AddItem(XmlElement config, string name, long value)
{
XmlElement item = AddItem(config, name, typeof(long));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, long value, AddItemObject callBack)
{
return AddItem(config, name, value);
}
public XmlElement AddItem(XmlElement config, string name, float value)
{
XmlElement item = AddItem(config, name, typeof(float));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, float value, AddItemObject callBack)
{
return AddItem(config, name, value);
}
public XmlElement AddItem(XmlElement config, string name, double value)
{
XmlElement item = AddItem(config, name, typeof(double));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, double value, AddItemObject callBack)
{
return AddItem(config, name, value);
}
public XmlElement AddItem(XmlElement config, string name, char value)
{
XmlElement item = AddItem(config, name, typeof(char));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, char value, AddItemObject callBack)
{
return AddItem(config, name, value);
}
public XmlElement AddItem(XmlElement config, string name, bool value)
{
XmlElement item = AddItem(config, name, typeof(bool));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, bool value, AddItemObject callBack)
{
return AddItem(config, name, value);
}
public XmlElement AddItem<T>(XmlElement config, string name, IList<T> value)
{
return AddItem(config, name, value, null);
}
public XmlElement AddItem<T>(XmlElement config, string name, List<T> value, AddItemObject callBack)
{
XmlElement item = AddItem(config, name, typeof(IList));
if (value != null && value.Count > 0)
{
foreach (T childValue in value)
{
AddItem(item, null, childValue, callBack);
}
}
return item;
}
public XmlElement AddItem<K, V>(XmlElement config, string name, Dictionary<K, V> value)
{
return AddItem(config, name, value, null);
}
public XmlElement AddItem<K, V>(XmlElement config, string name, Dictionary<K, V> value, AddItemObject callBack)
{
XmlElement item = AddItem(config, name, typeof(IDictionary));
if (value != null && value.Count > 0)
{
foreach (K key in value.Keys)
{
V childValue = value[key];
XmlElement itemChild = AddItem(item, null, childValue, callBack);
AddKeyAttribute(itemChild, key.ToString());
}
}
return item;
}
public XmlElement AddItem(XmlElement config, string name, object value)
{
return AddItem(config, name, value, null);
}
public virtual XmlElement AddItem(XmlElement config, string name, object value, AddItemObject callBack)
{
XmlElement item = null;
if (value != null)
{
if (value is string)
{
item = AddItem(config, name, (string)value);
}
else if (value is int)
{
item = AddItem(config, name, (int)value);
}
else if (value is long)
{
item = AddItem(config, name, (long)value);
}
else if (value is float)
{
item = AddItem(config, name, (float)value);
}
else if (value is double)
{
item = AddItem(config, name, (double)value);
}
else if (value is char)
{
item = AddItem(config, name, (char)value);
}
else if (value is bool)
{
item = AddItem(config, name, (bool)value);
}
else if (value is IList)
{
IList list = (IList)value;
item = AddItem(config, name, typeof(IList));
if (value != null && list.Count > 0)
{
foreach (object childValue in list)
{
AddItem(item, null, childValue, callBack);
}
}
}
else if (value is IDictionary)
{
IDictionary dictionary = (IDictionary)value;
item = AddItem(config, name, typeof(IDictionary));
if (value != null && dictionary.Count > 0)
{
foreach (object key in dictionary.Keys)
{
object childValue = dictionary[key];
XmlElement itemChild = AddItem(item, null, childValue, callBack);
AddKeyAttribute(itemChild, key.ToString());
}
}
}
else if (callBack != null)
{
item = callBack(this, config, name, value);
}
else
{
item = AddItem(config, name, value.GetType());
}
}
else
{
item = AddItem(config, name);
}
return item;
}
public XmlAttribute AddNameAttribute(XmlElement item, string name)
{
return AddAttribute(item, TagName, name);
}
public XmlAttribute AddTypeAttribute(XmlElement item, Type type)
{
return AddAttribute(item, TagTypeName, type.Name);
}
public XmlAttribute AddKeyAttribute(XmlElement item, string key)
{
return AddAttribute(item, TagKey, key);
}
public XmlAttribute AddAttribute(XmlElement item, string name, string value)
{
XmlAttribute attribute = doc.CreateAttribute(name);
attribute.Value = value;
item.Attributes.Append(attribute);
return attribute;
}
public virtual XmlElement GetConfig(string name)
{
foreach (XmlElement config in doc.DocumentElement.ChildNodes)
{
if (name != null && name.Equals(config.Attributes[TagName].Value))
{
return config;
}
}
return null;
}
public XmlElement GetItem(XmlElement config, string name)
{
if (config != null)
{
foreach (XmlElement item in config.ChildNodes)
{
if (name != null && name.Equals(item.Attributes[TagName].Value))
{
return item;
}
}
}
return null;
}
public XmlElement GetItem(string configName, string ItemName)
{
return GetItem(GetConfig(configName), ItemName);
}
public object GetItemValue(string configName, string itemName)
{
return GetItemValue(configName, itemName, null);
}
public object GetItemValue(string configName, string itemName, GetItemValueObject callBack)
{
return GetItemValue(GetConfig(configName), itemName, callBack);
}
public object GetItemValue(XmlElement config, string itemName)
{
return GetItemValue(config, itemName, null);
}
public object GetItemValue(XmlElement config, string itemName, GetItemValueObject callBack)
{
return GetItemValue(GetItem(config, itemName), callBack);
}
public object GetItemValue(XmlElement item)
{
return GetItemValue(item, (GetItemValueObject)null);
}
public virtual object GetItemValue(XmlElement item, GetItemValueObject callBack)
{
if (item != null && item.Attributes[TagTypeName] != null)
{
string typeName = item.Attributes[TagTypeName].Value;
if (typeName != null)
{
if (typeName.Equals(typeof(string).Name))
{
return item.FirstChild == null ? null : item.FirstChild.InnerText;
}
else if (typeName.Equals(typeof(int).Name))
{
return item.FirstChild == null ? null : (object)int.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(long).Name))
{
return item.FirstChild == null ? null : (object)long.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(float).Name))
{
return item.FirstChild == null ? null : (object)float.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(double).Name))
{
return item.FirstChild == null ? null : (object)double.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(char).Name))
{
return item.FirstChild == null ? null : (object)char.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(bool).Name))
{
return item.FirstChild == null ? null : (object)bool.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(IList).Name))
{
List<object> value = new List<object>();
foreach (XmlElement itemChild in item.ChildNodes)
{
object childValue = GetItemValue(itemChild, callBack);
value.Add(childValue);
}
return value;
}
else if (typeName.Equals(typeof(IDictionary).Name))
{
Dictionary<string, object> value = new Dictionary<string, object>();
foreach (XmlElement itemChild in item.ChildNodes)
{
string key = itemChild.Attributes[TagKey].InnerText;
object childValue = GetItemValue(itemChild, callBack);
value[key] = childValue;
}
return value;
}
}
if (callBack != null)
{
return callBack(this, item, typeName);
}
}
return null;
}
public bool Load()
{
try
{
doc.Load(filePath);
}
catch
{
return false;
}
return true;
}
public bool Save()
{
try
{
new FileInfo(filePath).Directory.Create();
doc.Save(filePath);
}
catch
{
return false;
}
return true;
}
public bool Save(string filePath)
{
this.filePath = filePath;
return Save();
}
public string SaveToXmlStr()
{
StringWriter str = new StringWriter();
doc.Save(str);
try
{
return str.ToString();
}
catch
{
return "";
}
}
protected static string TagConfigs = "Configs";
protected static string TagConfig = "Config";
protected static string TagItem = "Item";
protected static string TagName = "name";
protected static string TagTypeName = "typeName";
protected static string TagKey = "key";
protected XmlDocument doc = new XmlDocument();
protected string filePath;
}
/// <summary>
/// 扩展之前的XmlConfigFile 添加了Color类型
/// by:Gene
/// </summary>
public class MyXmlConfigFile : XmlConfigFile
{
public static bool IsGType(Type type)
{
if (type == null) return false;
return type.Equals(typeof(Byte))
|| type.Equals(typeof(SByte))
|| type.Equals(typeof(UInt16))
|| type.Equals(typeof(Int32))
|| type.Equals(typeof(UInt32))
|| type.Equals(typeof(Int64))
|| type.Equals(typeof(UInt64))
|| type.Equals(typeof(Single))
|| type.Equals(typeof(Double))
|| type.Equals(typeof(decimal))
|| type.Equals(typeof(string))
|| type.Equals(typeof(bool))
|| type.Equals(typeof(char))
|| type.Equals(typeof(DateTime))
|| type.Equals(typeof(System.Drawing.Point))
|| type.Equals(typeof(System.Drawing.PointF))
|| type.Equals(typeof(System.Drawing.Font))
|| type.Equals(typeof(System.Drawing.Size))
|| type.Equals(typeof(System.Drawing.SizeF))
|| type.Equals(typeof(System.Drawing.Rectangle))
|| type.Equals(typeof(System.Drawing.RectangleF))
|| type.Equals(typeof(System.Drawing.Color));
}
public MyXmlConfigFile():base()
{
}
public MyXmlConfigFile(string filePath):base(filePath)
{
}
public XmlComment AddComment(string txt)
{
XmlComment xc = doc.CreateComment(txt);
doc.DocumentElement.AppendChild(xc);
return xc;
}
public XmlComment AddComment(XmlElement config, string txt)
{
XmlComment xc = doc.CreateComment(txt);
config.AppendChild(xc);
return xc;
}
public XmlElement AddItem(XmlElement config, string name, uint value)
{
XmlElement item = AddItem(config, name, typeof(uint));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, short value)
{
XmlElement item = AddItem(config, name, typeof(short));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, ushort value)
{
XmlElement item = AddItem(config, name, typeof(ushort));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, byte value)
{
XmlElement item = AddItem(config, name, typeof(byte));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, DateTime value)
{
XmlElement item = AddItem(config, name, typeof(DateTime));
item.AppendChild(doc.CreateTextNode(value.Ticks.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, ulong value)
{
XmlElement item = AddItem(config, name, typeof(ulong));
item.AppendChild(doc.CreateTextNode(value.ToString()));
return item;
}
public XmlElement AddItem(XmlElement config, string name, System.Drawing.Color value)
{
XmlElement item = AddItem(config, name, typeof(System.Drawing.Color));
item.AppendChild(doc.CreateTextNode(value.A+","+value.R+","+value.G+","+value.B));
return item;
}
public XmlElement AddItem(XmlElement config, string name, System.Drawing.Point value)
{
XmlElement item = AddItem(config, name, typeof(System.Drawing.Point));
item.AppendChild(doc.CreateTextNode(value.X + "," + value.Y ));
return item;
}
public XmlElement AddItem(XmlElement config, string name, System.Drawing.Size value)
{
XmlElement item = AddItem(config, name, typeof(System.Drawing.Size));
item.AppendChild(doc.CreateTextNode(value.Width + "," + value.Height));
return item;
}
public XmlElement AddItem(XmlElement config, string name, System.Drawing.Rectangle value)
{
XmlElement item = AddItem(config, name, typeof(System.Drawing.Rectangle));
item.AppendChild(doc.CreateTextNode(value.X + "," + value.Y + "," + value.Width + "," + value.Height));
return item;
}
public XmlElement AddItem(XmlElement config, string name, System.Drawing.PointF value)
{
XmlElement item = AddItem(config, name, typeof(System.Drawing.PointF));
item.AppendChild(doc.CreateTextNode(value.X + "," + value.Y));
return item;
}
public XmlElement AddItem(XmlElement config, string name, System.Drawing.SizeF value)
{
XmlElement item = AddItem(config, name, typeof(System.Drawing.SizeF));
item.AppendChild(doc.CreateTextNode(value.Width + "," + value.Height));
return item;
}
public XmlElement AddItem(XmlElement config, string name, System.Drawing.RectangleF value)
{
XmlElement item = AddItem(config, name, typeof(System.Drawing.RectangleF));
item.AppendChild(doc.CreateTextNode(value.X+","+value.Y+","+value.Width + "," + value.Height));
return item;
}
public XmlElement AddItem(XmlElement config, string name, System.Drawing.Font value)
{
XmlElement item = AddItem(config, name, typeof(System.Drawing.Font));
item.AppendChild(doc.CreateTextNode(
value.FontFamily.Name + ","
+ value.Size ));
return item;
}
public override XmlElement AddItem(XmlElement config, string name, object value, AddItemObject callBack)
{
XmlElement item = null;
if (value != null)
{
if (value is string)
{
item = AddItem(config, name, (string)value);
}
else if (value is int)
{
item = AddItem(config, name, (int)value);
}
else if (value is byte)
{
item = AddItem(config, name, (byte)value);
}
else if (value is short)
{
item = AddItem(config, name, (short)value);
}
else if (value is long)
{
item = AddItem(config, name, (long)value);
}
else if (value is uint)
{
item = AddItem(config, name, (uint)value);
}
else if (value is ushort)
{
item = AddItem(config, name, (ushort)value);
}
else if (value is ulong)
{
item = AddItem(config, name, (ulong)value);
}
else if (value is float)
{
item = AddItem(config, name, (float)value);
}
else if (value is double)
{
item = AddItem(config, name, (double)value);
}
else if (value is char)
{
item = AddItem(config, name, (char)value);
}
else if (value is bool)
{
item = AddItem(config, name, (bool)value);
}
else if (value is DateTime)
{
item = AddItem(config, name, (DateTime)value);
}
else if (value is System.Drawing.Color)
{
item = AddItem(config, name, (System.Drawing.Color)value);
}
else if (value is System.Drawing.Point)
{
item = AddItem(config, name, (System.Drawing.Point)value);
}
else if (value is System.Drawing.Size)
{
item = AddItem(config, name, (System.Drawing.Size)value);
}
else if (value is System.Drawing.Rectangle)
{
item = AddItem(config, name, (System.Drawing.Rectangle)value);
}
else if (value is System.Drawing.PointF)
{
item = AddItem(config, name, (System.Drawing.PointF)value);
}
else if (value is System.Drawing.SizeF)
{
item = AddItem(config, name, (System.Drawing.SizeF)value);
}
else if (value is System.Drawing.RectangleF)
{
item = AddItem(config, name, (System.Drawing.RectangleF)value);
}
else if (value is System.Drawing.Font)
{
item = AddItem(config, name, (System.Drawing.Font)value);
}
else if (value is IList)
{
IList list = (IList)value;
item = AddItem(config, name, typeof(IList));
if (value != null && list.Count > 0)
{
foreach (object childValue in list)
{
AddItem(item, null, childValue, callBack);
}
}
}
else if (value is IDictionary)
{
IDictionary dictionary = (IDictionary)value;
item = AddItem(config, name, typeof(IDictionary));
if (value != null && dictionary.Count > 0)
{
foreach (object key in dictionary.Keys)
{
object childValue = dictionary[key];
XmlElement itemChild = AddItem(item, null, childValue, callBack);
AddKeyAttribute(itemChild, key.ToString());
}
}
}
else if (callBack != null)
{
item = callBack(this, config, name, value);
}
else
{
item = AddItem(config, name, value.GetType());
}
}
else
{
item = AddItem(config, name);
}
return item;
}
public override XmlElement GetConfig(string name)
{
foreach (XmlNode xmlNode in doc.DocumentElement.ChildNodes)
{
if (xmlNode is XmlElement && name != null && name.Equals(xmlNode.Attributes[TagName].Value))
{
return xmlNode as XmlElement;
}
}
return null;
}
public override object GetItemValue(XmlElement item, GetItemValueObject callBack)
{
if (item != null && item.Attributes[TagTypeName] != null)
{
string typeName = item.Attributes[TagTypeName].Value;
if (typeName != null)
{
if (typeName.Equals(typeof(string).Name))
{
return item.FirstChild == null ? null : item.FirstChild.InnerText;
}
else if (typeName.Equals(typeof(int).Name))
{
return item.FirstChild == null ? null : (object)int.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(byte).Name))
{
return item.FirstChild == null ? null : (object)byte.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(short).Name))
{
return item.FirstChild == null ? null : (object)short.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(long).Name))
{
return item.FirstChild == null ? null : (object)long.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(uint).Name))
{
return item.FirstChild == null ? null : (object)uint.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(ushort).Name))
{
return item.FirstChild == null ? null : (object)ushort.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(ulong).Name))
{
return item.FirstChild == null ? null : (object)ulong.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(float).Name))
{
return item.FirstChild == null ? null : (object)float.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(double).Name))
{
return item.FirstChild == null ? null : (object)double.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(char).Name))
{
return item.FirstChild == null ? null : (object)char.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(bool).Name))
{
return item.FirstChild == null ? null : (object)bool.Parse(item.FirstChild.InnerText);
}
else if (typeName.Equals(typeof(DateTime).Name))
{
return item.FirstChild == null ? null : (object)new DateTime(long.Parse(item.FirstChild.InnerText));
}
else if (typeName.Equals(typeof(System.Drawing.Color).Name))
{
if (item.FirstChild == null) return null;
string[] strs = item.FirstChild.InnerText.Split(',');
if (strs.Length < 4) return null;
System.Drawing.Color col = System.Drawing.Color.FromArgb(
int.Parse(strs[0]),
int.Parse(strs[1]),
int.Parse(strs[2]),
int.Parse(strs[3]));
return col;
}
else if (typeName.Equals(typeof(System.Drawing.Point).Name))
{
string[] strs = item.FirstChild.InnerText.Split(',');
if (strs.Length < 2) return null;
System.Drawing.Point p = new System.Drawing.Point(int.Parse(strs[0]),
int.Parse(strs[1]));
return p;
}
else if (typeName.Equals(typeof(System.Drawing.Size).Name))
{
string[] strs = item.FirstChild.InnerText.Split(',');
if (strs.Length < 2) return null;
System.Drawing.Size p = new System.Drawing.Size(int.Parse(strs[0]),
int.Parse(strs[1]));
return p;
}
else if (typeName.Equals(typeof(System.Drawing.Rectangle).Name))
{
string[] strs = item.FirstChild.InnerText.Split(',');
if (strs.Length < 4) return null;
System.Drawing.Rectangle p = new System.Drawing.Rectangle(int.Parse(strs[0]),
int.Parse(strs[1]), int.Parse(strs[2]), int.Parse(strs[3]));
return p;
}
else if (typeName.Equals(typeof(System.Drawing.PointF).Name))
{
string[] strs = item.FirstChild.InnerText.Split(',');
if (strs.Length < 2) return null;
System.Drawing.PointF p = new System.Drawing.PointF(float.Parse(strs[0]),
float.Parse(strs[1]));
return p;
}
else if (typeName.Equals(typeof(System.Drawing.SizeF).Name))
{
string[] strs = item.FirstChild.InnerText.Split(',');
if (strs.Length < 2) return null;
System.Drawing.SizeF p = new System.Drawing.SizeF(float.Parse(strs[0]),
float.Parse(strs[1]));
return p;
}
else if (typeName.Equals(typeof(System.Drawing.RectangleF).Name))
{
string[] strs = item.FirstChild.InnerText.Split(',');
if (strs.Length < 4) return null;
System.Drawing.RectangleF p = new System.Drawing.RectangleF(int.Parse(strs[0]),
int.Parse(strs[1]), int.Parse(strs[2]), int.Parse(strs[3]));
return p;
}
else if (typeName.Equals(typeof(System.Drawing.Font).Name))
{
string[] strs = item.FirstChild.InnerText.Split(',');
if (strs.Length < 2) return null;
System.Drawing.Font p = new System.Drawing.Font(strs[0], float.Parse(strs[1]));
return p;
}
else if (typeName.Equals(typeof(IList).Name))
{
List<object> value = new List<object>();
foreach (XmlElement itemChild in item.ChildNodes)
{
object childValue = GetItemValue(itemChild, callBack);
value.Add(childValue);
}
return value;
}
else if (typeName.Equals(typeof(IDictionary).Name))
{
Dictionary<string, object> value = new Dictionary<string, object>();
foreach (XmlElement itemChild in item.ChildNodes)
{
string key = itemChild.Attributes[TagKey].InnerText;
object childValue = GetItemValue(itemChild, callBack);
value[key] = childValue;
}
return value;
}
}
if (callBack != null)
{
return callBack(this, item, typeName);
}
}
return null;
}
}
#endregion