static object CreateObj(TypeData td, BinaryReader bin) { Type t = td.ClassType; int size = (int)(bin.BaseStream.Length - bin.BaseStream.Position); if (size < td.Size / td.ArrayCount) { LogMgr.Log("字节流的长度小于类的长度, Bytes Size:" + bin.BaseStream.Length.ToString() + ", Class Size:" + (td.Size / td.ArrayCount).ToString()); return(null); } object obj = Activator.CreateInstance(t); if (obj == null) { return(null); } for (int i = 0; i < td.Fields.Length; ++i) { TypeData curData = td.Fields[i]; if (curData.Handle.FieldType.IsPrimitive) { ITypeReader iReader = TypeReaderMapping.GetReaderFromHash(curData.HashCode); curData.Handle.SetValue(obj, iReader.Read(bin, 0)); } else if (curData.Handle.FieldType.IsArray) { if (curData.VarType) { //变长 int curSize = (int)(bin.BaseStream.Length - bin.BaseStream.Position); if (curData.VarType && (curSize % curData.OrgSize != 0)) { LogMgr.Log("字节长度不是变长类型的整数倍, Bytes Size:" + curSize.ToString() + ", Class Size:" + td.OrgSize.ToString()); return(null); } if (curSize > 0) { curSize /= curData.OrgSize; if (curData.ClassType.IsPrimitive) { ITypeReader iReader = TypeReaderMapping.GetReaderFromHash(curData.HashCode); curData.Handle.SetValue(obj, iReader.Read(bin, curSize)); } else { object[] arr = (object[])Array.CreateInstance(curData.ClassType, curSize); for (int j = 0; j < curSize; ++j) { arr[j] = CreateObj(curData, bin); if (arr[j] == null) { return(null); } } curData.Handle.SetValue(obj, arr); } } } else { //固定长度 if (curData.ClassType.IsPrimitive) { ITypeReader iReader = TypeReaderMapping.GetReaderFromHash(curData.HashCode); curData.Handle.SetValue(obj, iReader.Read(bin, curData.ArrayCount)); } else { object[] arr = (object[])Array.CreateInstance(curData.ClassType, curData.ArrayCount); for (int j = 0; j < curData.ArrayCount; ++j) { arr[j] = CreateObj(curData, bin); if (arr[j] == null) { return(null); } } curData.Handle.SetValue(obj, arr); } } } else if (curData.HashCode == TypeMapping.STRING_HASH) { ITypeReader iReader = TypeReaderMapping.GetReaderFromHash(curData.HashCode); if (curData.VarType) { int curSize = (int)(bin.BaseStream.Length - bin.BaseStream.Position); if (curSize % 2 != 0) { LogMgr.Log("字符串长度不是字符的整数倍:" + curSize.ToString()); return(null); } if (curSize > 0) { curData.Handle.SetValue(obj, iReader.Read(bin, curSize / 2)); } } else { curData.Handle.SetValue(obj, iReader.Read(bin, curData.ArrayCount)); } } else if (curData.Handle.FieldType.IsClass /*|| curData.FieldHandle.FieldType.IsValueType*/) { object subObj = CreateObj(curData, bin); if (subObj == null) { return(null); } curData.Handle.SetValue(obj, subObj); } else { LogMgr.Log("未知的类型:" + curData.Handle.FieldType.ToString()); return(null); } } return(obj); }
public static TypeData RegisterClass(Type t) { int fieldCount; List <TypeSortedField> sortFields = SortFields(t.GetFields(), out fieldCount); if (sortFields == null) { return(null); } TypeData td = new TypeData(); td.ClassType = t; td.HashCode = Utility.GetHash(td.ClassType.ToString()); td.Handle = null; td.Size = 0; td.ArrayCount = 1; td.Fields = new TypeData[fieldCount]; int idx = 0; for (int n = 0; n < sortFields.Count; ++n) { for (int i = 0; i < sortFields[n].Fields.Count; ++i) { TypeFiledPack field = sortFields[n].Fields[i]; FieldInfo fi = field.Field; TypeData curData = null; if (fi.FieldType.IsPrimitive) { curData = new TypeData(); curData.Handle = fi; curData.ArrayCount = 1; curData.ClassType = fi.FieldType; curData.HashCode = Utility.GetHash(curData.ClassType.ToString()); curData.OrgSize = curData.Size = TypeReaderMapping.GetReaderFromHash(curData.HashCode).TypeSize(); } else if (fi.FieldType.IsArray) { //获取原始类型 string strType = fi.FieldType.ToString(); strType = strType.Remove(strType.Length - 2, 2); Type orgType = Type.GetType(strType); if (orgType == null) { LogMgr.Log("未知的数组类型:" + strType); return(null); } if (field.Count < 1 && field.VarType == false) { LogMgr.Log("数组的特性TypeInfo必须大于0,或者是变长的数组"); return(null); } if (orgType.IsPrimitive) { curData = new TypeData(); curData.ClassType = orgType; curData.HashCode = Utility.GetHash(curData.ClassType.ToString()); curData.ArrayCount = field.Count; curData.VarType = field.VarType; curData.OrgSize = curData.Size = TypeReaderMapping.GetReaderFromHash(curData.HashCode).TypeSize(); if (curData.VarType) { curData.Size = 0; } else { curData.Size = (short)(curData.Size * field.Count); } curData.Handle = fi; } else if (orgType.IsClass /* || orgType.IsValueType*/) { curData = RegisterClass(orgType); if (curData == null) { return(null); } curData.ArrayCount = field.Count; curData.VarType = field.VarType; if (curData.VarType) { curData.Size = 0; } else { curData.Size = (short)(curData.Size * field.Count); } curData.Handle = fi; } else { LogMgr.Log("未知的数组原始类型:" + strType); return(null); } } else if (fi.FieldType == typeof(string)) { curData = new TypeData(); curData.ArrayCount = field.Count; curData.OrgSize = (short)sizeof(char); curData.VarType = field.VarType; if (curData.VarType) { curData.Size = 0; } else { curData.Size = (short)(sizeof(char) * field.Count); } curData.ClassType = fi.FieldType; curData.HashCode = STRING_HASH; curData.Handle = fi; } else if (fi.FieldType.IsClass /*|| fi.FieldType.IsValueType*/) { curData = RegisterClass(fi.FieldType); if (curData == null) { return(null); } curData.Handle = fi; } else { LogMgr.Log("未知的类型:" + fi.FieldType.ToString()); return(null); } td.Fields[idx++] = curData; td.Size += curData.Size; } } td.OrgSize = td.Size; return(td); }
static bool ConvertToBytes(TypeData td, object obj, BinaryWriter bin) { for (int i = 0; i < td.Fields.Length; ++i) { TypeData curData = td.Fields[i]; if (curData.Handle.FieldType.IsPrimitive) { ITypeReader iReader = TypeReaderMapping.GetReaderFromHash(curData.HashCode); object f = curData.Handle.GetValue(obj); iReader.Write(bin, f, 0); } else if (curData.Handle.FieldType.IsArray) { if (curData.ClassType.IsPrimitive) { ITypeReader iReader = TypeReaderMapping.GetReaderFromHash(curData.HashCode); object f = curData.Handle.GetValue(obj); if (curData.VarType)//写入变长 { iReader.Write(bin, f, -1); } else { iReader.Write(bin, f, curData.ArrayCount); } } else { object[] f = (object[])curData.Handle.GetValue(obj); if (curData.VarType)//写入变长 { if (f != null && f.Length > 0) { for (int j = 0; j < f.Length; ++j) { ConvertToBytes(curData, f[j], bin); } } } else { for (int j = 0; j < curData.ArrayCount; ++j) { ConvertToBytes(curData, f[j], bin); } } } } else if (curData.HashCode == TypeMapping.STRING_HASH) { ITypeReader iReader = TypeReaderMapping.GetReaderFromHash(curData.HashCode); object f = curData.Handle.GetValue(obj); if (curData.VarType) { iReader.Write(bin, f, -1); } else { iReader.Write(bin, f, curData.ArrayCount); } } else if (curData.Handle.FieldType.IsClass /* || curData.Handle.FieldType.IsValueType*/) { object f = curData.Handle.GetValue(obj); ConvertToBytes(curData, f, bin); } else { LogMgr.Log("未知的类型:" + curData.Handle.FieldType.ToString()); return(false); } } return(true); }