static public NeedToSave GetSaveAttribute(object pTarget, string pFieldName) { NeedToSave ret = null; System.Type ttype = pTarget.GetType(); DataType tselftype = GetDataTypeSysType(ttype); DataType tchildType = DataType.baseType; DataType tkeyType = DataType.baseType; if (ttype.FullName.Contains(".List")) { System.Type[] genericArgTypes = ttype.GetGenericArguments(); if (genericArgTypes != null && genericArgTypes.Length > 0) { tchildType = GetDataTypeSysType(genericArgTypes[0]); } } else if (ttype.FullName.Contains(".Dictionary")) { System.Type[] genericArgTypes = ttype.GetGenericArguments(); if (genericArgTypes != null && genericArgTypes.Length > 0) { tkeyType = GetDataTypeSysType(genericArgTypes[0]); tchildType = GetDataTypeSysType(genericArgTypes[1]); } } ret = new NeedToSave(pFieldName, tselftype, tchildType, tkeyType); return(ret); }
static private void SaveBaseObject(LitEngine.IO.AESWriter pWriter, NeedToSave pAtt, object pTarget) { FieldType ttype = GetValueType(pTarget); if (ttype == FieldType.Null) { throw new System.ArgumentNullException("未知类型." + pAtt.FieldName); } SaveByType(pWriter, ttype, pTarget); }
static private void SaveList(LitEngine.IO.AESWriter pWriter, NeedToSave pAtt, object pTarget) { pWriter.WriteBool(pTarget != null); if (pTarget == null) { return; } ArrayList tlist = new ArrayList((ICollection)pTarget); pWriter.WriteString(pTarget.GetType().FullName); pWriter.WriteInt(tlist.Count); for (int i = 0; i < tlist.Count; i++) { var item = tlist[i]; SaveByField(pWriter, new NeedToSave(i.ToString(), pAtt.childType), item); } }
static private void SaveDataObject(LitEngine.IO.AESWriter pWriter, NeedToSave pAtt, object pTarget) { pWriter.WriteBool(pTarget != null); if (pTarget == null) { return; } System.Type ttype = pTarget.GetType(); var tfields = ttype.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); List <SaveValue> tfieldlist = new List <SaveValue>(); for (int i = 0; i < tfields.Length; i++) { FieldInfo tinfo = tfields[i]; NeedToSave tetst = tinfo.GetCustomAttribute(typeof(NeedToSave)) as NeedToSave; object tvalue = tinfo.GetValue(pTarget); if (tetst != null && tvalue != null) { SaveValue item = new SaveValue() { info = tinfo, value = tvalue }; tfieldlist.Add(item); } } pWriter.WriteString(ttype.FullName); pWriter.WriteInt(tfieldlist.Count); for (int i = 0; i < tfieldlist.Count; i++) { SaveValue tinfo = tfieldlist[i]; NeedToSave tatt = DataUtil.GetSaveAttribute(tinfo.value, tinfo.info.Name); SaveByField(pWriter, tatt, tinfo.value); } }
static private void SaveByField(LitEngine.IO.AESWriter pWriter, NeedToSave pAtt, object pTarget) { pWriter.WriteInt((int)pAtt.dataType); pWriter.WriteString(pAtt.FieldName); switch (pAtt.dataType) { case DataType.baseType: SaveBaseObject(pWriter, pAtt, pTarget); break; case DataType.DataObject: SaveDataObject(pWriter, pAtt, pTarget); break; case DataType.List: SaveList(pWriter, pAtt, pTarget); break; case DataType.Dictionary: SaveDictionary(pWriter, pAtt, pTarget); break; } }
static private void SaveDictionary(LitEngine.IO.AESWriter pWriter, NeedToSave pAtt, object pTarget) { pWriter.WriteBool(pTarget != null); if (pTarget == null) { return; } System.Type tarType = pTarget.GetType(); MethodInfo tm = tarType.GetMethod("get_Item", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); PropertyInfo tcinfo = tarType.GetProperty("Keys"); ArrayList tlist = new ArrayList((ICollection)tcinfo.GetValue(pTarget)); pWriter.WriteString(pTarget.GetType().FullName); pWriter.WriteInt(tlist.Count); for (int i = 0; i < tlist.Count; i++) { var tkey = tlist[i]; object tvalue = tm.Invoke(pTarget, new object[] { tkey }); string tname = i.ToString(); SaveByField(pWriter, new NeedToSave(tname, pAtt.keyType), tkey); SaveByField(pWriter, new NeedToSave(tname, pAtt.childType), tvalue); } }