コード例 #1
0
 private static object DeserializeObject(BitConverterEx converter, object instance, List <FieldInfo> fields, bool useBigEnum)
 {
     try
     {
         foreach (FieldInfo field in fields)
         {
             Type fieldType = field.FieldType;
             if (fieldType == byteType)
             {
                 field.SetValue(instance, converter.PopByte());
             }
             else if (fieldType == boolType)
             {
                 field.SetValue(instance, converter.PopBoolean());
             }
             else if (fieldType == shortType)
             {
                 field.SetValue(instance, converter.PopShort());
             }
             else if (fieldType == intType)
             {
                 field.SetValue(instance, converter.PopInteger());
             }
             else if (fieldType == longType)
             {
                 field.SetValue(instance, converter.PopLong());
             }
             else if (fieldType == floatType)
             {
                 field.SetValue(instance, converter.PopFloat());
             }
             else if (fieldType == doubleType)
             {
                 field.SetValue(instance, converter.PopDouble());
             }
             else if (fieldType == stringType)
             {
                 field.SetValue(instance, converter.PopString());
             }
             else if (fieldType.IsEnum)
             {
                 if (useBigEnum)
                 {
                     field.SetValue(instance, converter.PopInteger());
                 }
                 else
                 {
                     field.SetValue(instance, converter.PopByte());
                 }
             }
             else if (fieldType.IsArray)
             {
                 Type elemType = fieldType.GetElementType();
                 if (elemType == byteType)
                 {
                     field.SetValue(instance, converter.PopByteArray());
                 }
                 else if (elemType == boolType)
                 {
                     field.SetValue(instance, converter.PopBooleanArray());
                 }
                 else if (elemType == shortType)
                 {
                     field.SetValue(instance, converter.PopShortArray());
                 }
                 else if (elemType == intType)
                 {
                     field.SetValue(instance, converter.PopIntegerArray());
                 }
                 else if (elemType == longType)
                 {
                     field.SetValue(instance, converter.PopLongArray());
                 }
                 else if (elemType == floatType)
                 {
                     field.SetValue(instance, converter.PopFloatArray());
                 }
                 else if (elemType == doubleType)
                 {
                     field.SetValue(instance, converter.PopDoubleArray());
                 }
                 else if (elemType == stringType)
                 {
                     field.SetValue(instance, converter.PopStringArray());
                 }
                 else if (elemType.IsEnum)
                 {
                     int   arraySize = (ushort)converter.PopShort();
                     Array array     = Array.CreateInstance(elemType, arraySize);
                     for (int i = 0; i < arraySize; ++i)
                     {
                         if (useBigEnum)
                         {
                             array.SetValue(converter.PopInteger(), i);
                         }
                         else
                         {
                             array.SetValue(converter.PopByte(), i);
                         }
                     }
                     field.SetValue(instance, array);
                 }
                 else if (elemType.IsClass || elemType.IsValueType)
                 {
                     int              arraySize = (ushort)converter.PopShort();
                     Array            array     = Array.CreateInstance(elemType, arraySize);
                     List <FieldInfo> subFields = null;
                     if (tempFieldListPool.Count > 0)
                     {
                         subFields = tempFieldListPool.Dequeue();
                     }
                     else
                     {
                         subFields = new List <FieldInfo>();
                     }
                     if (FindAllFields(elemType, ref subFields))
                     {
                         for (int i = 0; i < arraySize; ++i)
                         {
                             object o = Activator.CreateInstance(elemType);
                             DeserializeObject(converter, o, subFields, useBigEnum);
                             array.SetValue(o, i);
                         }
                     }
                     subFields.Clear();
                     tempFieldListPool.Enqueue(subFields);
                     field.SetValue(instance, array);
                 }
             }
             else if (fieldType.IsClass || fieldType.IsValueType)
             {
                 object           o         = Activator.CreateInstance(fieldType);
                 List <FieldInfo> subFields = null;
                 if (tempFieldListPool.Count > 0)
                 {
                     subFields = tempFieldListPool.Dequeue();
                 }
                 else
                 {
                     subFields = new List <FieldInfo>();
                 }
                 if (FindAllFields(fieldType, ref subFields))
                 {
                     DeserializeObject(converter, o, subFields, useBigEnum);
                 }
                 subFields.Clear();
                 tempFieldListPool.Enqueue(subFields);
                 field.SetValue(instance, o);
             }
         }
     }
     catch (Exception ex)
     {
         if (LogUtil.ShowError != null)
         {
             LogUtil.ShowError(string.Format("DeserializeObject Exception at {0}\n{1}", instance.GetType().Name, ex.Message));
         }
     }
     return(instance);
 }