Example #1
0
 public virtual void ReadFromStream(BinaryReader reader)
 {
     FieldInfo[] fields = base.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
     FieldInfo[] array  = fields;
     for (int i = 0; i < array.Length; i++)
     {
         FieldInfo  fieldInfo  = array[i];
         MethodInfo methodInfo = (MethodInfo)MarshallingMethods.ReadMethods[fieldInfo.FieldType];
         if (fieldInfo.FieldType.IsArray)
         {
             Type elementType = fieldInfo.FieldType.GetElementType();
             if (elementType.IsValueType && elementType.IsPrimitive)
             {
                 if (elementType == typeof(char) || elementType == typeof(byte))
                 {
                     fieldInfo.SetValue(this, methodInfo.Invoke(reader, new object[]
                     {
                         this.GetFieldSize(fieldInfo)
                     }));
                 }
                 else
                 {
                     fieldInfo.SetValue(this, methodInfo.Invoke(null, new object[]
                     {
                         reader,
                         this.GetFieldSize(fieldInfo)
                     }));
                 }
             }
             else
             {
                 int fieldSize = this.GetFieldSize(fieldInfo);
                 methodInfo = (MethodInfo)MarshallingMethods.ReadMethods[typeof(CustomMarshaler)];
                 Array array2 = Array.CreateInstance(elementType, fieldSize);
                 for (int j = 0; j < fieldSize; j++)
                 {
                     array2.SetValue(Activator.CreateInstance(elementType), j);
                     methodInfo.Invoke(array2.GetValue(j), new object[]
                     {
                         reader
                     });
                 }
                 fieldInfo.SetValue(this, array2);
             }
         }
         else
         {
             if (fieldInfo.FieldType == typeof(string))
             {
                 fieldInfo.SetValue(this, methodInfo.Invoke(null, new object[]
                 {
                     reader,
                     this.GetFieldSize(fieldInfo)
                 }));
             }
             else
             {
                 if (fieldInfo.FieldType.IsValueType && fieldInfo.FieldType.IsPrimitive)
                 {
                     fieldInfo.SetValue(this, methodInfo.Invoke(reader, null));
                 }
                 else
                 {
                     CustomMarshaler customMarshaler = (CustomMarshaler)Activator.CreateInstance(fieldInfo.FieldType);
                     customMarshaler.ReadFromStream(reader);
                 }
             }
         }
     }
 }