public static object ReadObject(this ObjectReader reader, int index) { var type = reader.ReadType(); var value = FormatterServices.GetUninitializedObject(type); reader.ReferenceContainer[index] = value; var fields = SerializationHelper.GetSerializableMembers(type); foreach (var field in fields) { var fieldValue = reader.Deserialize(); var fieldInfo = field.Field; if (fieldValue != null && fieldInfo.FieldType.IsNullable()) { var enumType = fieldInfo.FieldType.GetGenericArguments()[0]; if (enumType.IsEnum) { fieldValue = Enum.ToObject(enumType, fieldValue); } } field.SetValue(value, fieldValue); } return(value); }
public static IDictionary ReadGDictionary(this ObjectReader reader, int index) { Type keyType = reader.ReadType() , valueType = reader.ReadType(); var count = reader.ReadInt32(); var value = Activator.CreateInstance(DefineTypes.GDictionary.MakeGenericType(keyType, valueType), count) as IDictionary; reader.ReferenceContainer[index] = value; for (int i = 0; i < count; i++) { value.Add(reader.Deserialize(), reader.Deserialize()); } return(value); }
public static object ReadCustom(this ObjectReader reader, int index) { var type = reader.ReadType(); //var byteLength = reader.ReadInt32(); //var bytes = byteLength == 0 ? new byte[0] : reader.ReadBuffer(byteLength); var customFormatter = Activator.CreateInstance(type, true) as ICustomSerializable; return(customFormatter.Deserialize(reader)); }
public static Type[] ReadTypeArray(this ObjectReader reader) { var length = reader.ReadInt32(); Type[] value = new Type[length]; for (int i = 0; i < length; i++) { value[i] = reader.ReadType(); } return(value); }
public static Array ReadSimpleArray(this ObjectReader reader, int index) { var elementType = reader.ReadType(); var length = reader.ReadInt32(); var value = Array.CreateInstance(elementType, length); reader.ReferenceContainer[index] = value; for (int i = 0; i < length; i++) { value.SetValue(reader.Deserialize(), i); } return(value); }
public static object ReadValueTypeObject(this ObjectReader reader) { var type = reader.ReadType(); var value = FormatterServices.GetUninitializedObject(type); var fields = SerializationHelper.GetSerializableMembers(type); foreach (var field in fields) { var fieldValue = reader.Deserialize(); field.SetValue(value, fieldValue); } return(value); }
public static IList ReadGList(this ObjectReader reader, int index) { var elementType = reader.ReadType(); var count = reader.ReadInt32(); var value = Activator.CreateInstance(DefineTypes.GList.MakeGenericType(elementType), count) as IList; reader.ReferenceContainer[index] = value; for (int i = 0; i < count; i++) { value.Add(reader.Deserialize()); } return(value); }
public static object ReadValueTypeObject(this ObjectReader reader) { var type = reader.ReadType(); var value = FormatterServices.GetUninitializedObject(type); Aoite.Reflection.Emitter.ValueTypeHolder holder = new Aoite.Reflection.Emitter.ValueTypeHolder(value); //- 值类型 var fields = SerializationHelper.GetSerializableMembers(type); foreach (var field in fields) { var fieldValue = reader.Deserialize(); field.SetValue(holder, fieldValue); } return(holder.Value); }
public static Result ReadGResult(this ObjectReader reader, int index) { var status = reader.ReadInt32(); var exceptionMessage = reader.ReadStringOrReference(); var elementType = reader.ReadType(); var value = Activator.CreateInstance(DefineTypes.GResult.MakeGenericType(elementType)) as Result; reader.ReferenceContainer[index] = value; value._Status = status; value._Message = exceptionMessage; var resultValue = reader.Deserialize(); value.SetValue(resultValue); return(value); }
public static Array ReadMultiRankArray(this ObjectReader reader, int index) { var elementType = reader.ReadType(); var rank = reader.ReadInt32(); Array value; int i, j; var des = new int[rank, 2]; var loc = new int[rank]; int[] rankLengths = new int[rank]; for (i = 0; i < rank; i++) { rankLengths[i] = reader.ReadInt32(); } value = Array.CreateInstance(elementType, rankLengths); reader.ReferenceContainer[index] = value; // 设置每一个 数组维 的上下标。 for (i = 0; i < rank; i++) { j = value.GetLowerBound(i); //- 上标 des[i, 0] = j; des[i, 1] = value.GetUpperBound(i); //- 下标 loc[i] = j; } i = rank - 1; while (loc[0] <= des[0, 1]) { value.SetValue(reader.Deserialize(), loc); loc[i]++; for (j = rank - 1; j > 0; j--) { if (loc[j] > des[j, 1]) { loc[j] = des[j, 0]; loc[j - 1]++; } } } return(value); }