Beispiel #1
0
 internal static object ReadConvertValue(object obj, string propertyValue, BinaryReader binaryReader)
 {
     if (binaryReader == null)
     {
         if (obj is string)
         {
             return(propertyValue);
         }
         if (obj is byte)
         {
             if (!string.IsNullOrEmpty(propertyValue))
             {
                 return((byte)propertyValue[0]);
             }
             return((byte)0);
         }
         if (obj is bool)
         {
             return(bool.Parse(propertyValue));
         }
         if (obj is long)
         {
             return(long.Parse(propertyValue, CultureInfo.InvariantCulture));
         }
         if (obj is int)
         {
             return(int.Parse(propertyValue, CultureInfo.InvariantCulture));
         }
         if (obj is float)
         {
             return(float.Parse(propertyValue, CultureInfo.InvariantCulture));
         }
         if (obj is double)
         {
             if (string.Compare(propertyValue, "Auto", StringComparison.OrdinalIgnoreCase) == 0)
             {
                 return(double.NaN);
             }
             return(double.Parse(propertyValue, CultureInfo.InvariantCulture));
         }
         if (obj is Font)
         {
             return((Font)fontConverter.ConvertFromString(null, CultureInfo.InvariantCulture, propertyValue));
         }
         if (obj is CultureInfo)
         {
             return(CultureInfo.GetCultureInfoByIetfLanguageTag(propertyValue));
         }
         if (obj is Color)
         {
             return((Color)colorConverter.ConvertFromString(null, CultureInfo.InvariantCulture, propertyValue));
         }
         if (obj is Enum)
         {
             return(Enum.Parse(obj.GetType(), propertyValue));
         }
         if (obj is Image)
         {
             return(ImageFromString(propertyValue));
         }
         if (obj is ShapeData)
         {
             return(ShapeData.ShapeDataFromString(propertyValue));
         }
         if (obj is Stream)
         {
             return(StreamFromString(propertyValue));
         }
         if (obj is double[])
         {
             string[] array  = propertyValue.Split(',');
             double[] array2 = new double[array.Length];
             for (int i = 0; i < array.Length; i++)
             {
                 array2[i] = double.Parse(array[i], CultureInfo.InvariantCulture);
             }
             return(array2);
         }
         if (obj is Size)
         {
             return((Size)sizeConverter.ConvertFromString(null, CultureInfo.InvariantCulture, propertyValue));
         }
         if (obj is DateTime)
         {
             return(XmlConvert.ToDateTime(propertyValue));
         }
         if (obj is Margins)
         {
             return(Margins.Parse(propertyValue));
         }
         if (obj is MapCoordinate)
         {
             return(MapCoordinate.Parse(propertyValue));
         }
         if (obj is Type)
         {
             return(Type.GetType(propertyValue));
         }
         throw new InvalidOperationException("Serializer do not support objects of type \"" + obj.GetType().ToString() + "\"");
     }
     if (obj is bool)
     {
         return(binaryReader.ReadBoolean());
     }
     if (obj is double)
     {
         return(binaryReader.ReadDouble());
     }
     if (obj is string)
     {
         return(binaryReader.ReadString());
     }
     if (obj is int)
     {
         return(binaryReader.ReadInt32());
     }
     if (obj is long)
     {
         return(binaryReader.ReadInt64());
     }
     if (obj is float)
     {
         return(binaryReader.ReadSingle());
     }
     if (obj is Enum)
     {
         return(Enum.Parse(obj.GetType(), binaryReader.ReadString()));
     }
     if (obj is byte)
     {
         return(binaryReader.ReadByte());
     }
     if (obj is Font)
     {
         return(fontConverter.ConvertFromString(null, CultureInfo.InvariantCulture, binaryReader.ReadString()));
     }
     if (obj is CultureInfo)
     {
         return(CultureInfo.GetCultureInfoByIetfLanguageTag(binaryReader.ReadString()));
     }
     if (obj is Color)
     {
         return(Color.FromArgb(binaryReader.ReadInt32()));
     }
     if (obj is DateTime)
     {
         return(DateTime.FromOADate(binaryReader.ReadDouble()));
     }
     if (obj is Margins)
     {
         return(new Margins(binaryReader.ReadInt32(), binaryReader.ReadInt32(), binaryReader.ReadInt32(), binaryReader.ReadInt32()));
     }
     if (obj is MapCoordinate)
     {
         return(new MapCoordinate(binaryReader.ReadDouble()));
     }
     if (obj is Type)
     {
         return(Type.GetType(binaryReader.ReadString()));
     }
     if (obj is Size)
     {
         return(new Size(binaryReader.ReadInt32(), binaryReader.ReadInt32()));
     }
     if (obj is double[])
     {
         double[] array3 = new double[binaryReader.ReadInt32()];
         for (int j = 0; j < array3.Length; j++)
         {
             array3[j] = binaryReader.ReadDouble();
         }
         return(array3);
     }
     if (obj is Image)
     {
         int          num          = binaryReader.ReadInt32();
         MemoryStream memoryStream = new MemoryStream(num + 10);
         memoryStream.Write(binaryReader.ReadBytes(num), 0, num);
         return(new Bitmap(Image.FromStream(memoryStream)));
     }
     throw new InvalidOperationException("Serializer do not support objects of type \"" + obj.GetType().ToString() + "\"");
 }
Beispiel #2
0
 private bool SetPropertyValue(object obj, PropertyInfo pi, BinaryReader reader)
 {
     if (pi != null)
     {
         object obj2 = null;
         if (pi.PropertyType == typeof(bool))
         {
             obj2 = reader.ReadBoolean();
         }
         else if (pi.PropertyType == typeof(double))
         {
             obj2 = reader.ReadDouble();
         }
         else if (pi.PropertyType == typeof(string))
         {
             obj2 = reader.ReadString();
         }
         else if (pi.PropertyType == typeof(int))
         {
             obj2 = reader.ReadInt32();
         }
         else if (pi.PropertyType == typeof(long))
         {
             obj2 = reader.ReadInt64();
         }
         else if (pi.PropertyType == typeof(float))
         {
             obj2 = reader.ReadSingle();
         }
         else if (pi.PropertyType.IsEnum)
         {
             obj2 = Enum.Parse(pi.PropertyType, reader.ReadString());
         }
         else if (pi.PropertyType == typeof(Font))
         {
             obj2 = SerializerBase.fontConverter.ConvertFromString(null, CultureInfo.InvariantCulture, reader.ReadString());
         }
         else if (pi.PropertyType == typeof(CultureInfo))
         {
             obj2 = CultureInfo.GetCultureInfoByIetfLanguageTag(reader.ReadString());
         }
         else if (pi.PropertyType == typeof(Color))
         {
             obj2 = Color.FromArgb(reader.ReadInt32());
         }
         else if (pi.PropertyType == typeof(DateTime))
         {
             obj2 = DateTime.FromOADate(reader.ReadDouble());
         }
         else if (pi.PropertyType == typeof(Margins))
         {
             obj2 = new Margins(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
         }
         else if (pi.PropertyType == typeof(MapCoordinate))
         {
             obj2 = new MapCoordinate(reader.ReadDouble());
         }
         else if (pi.PropertyType == typeof(Type))
         {
             obj2 = Type.GetType(reader.ReadString());
         }
         else if (pi.PropertyType == typeof(Size))
         {
             obj2 = new Size(reader.ReadInt32(), reader.ReadInt32());
         }
         else if (pi.PropertyType == typeof(double[]))
         {
             double[] array = new double[reader.ReadInt32()];
             for (int i = 0; i < array.Length; i++)
             {
                 array[i] = reader.ReadDouble();
             }
             obj2 = array;
         }
         else if (pi.PropertyType == typeof(Image))
         {
             int          num          = reader.ReadInt32();
             MemoryStream memoryStream = new MemoryStream(num + 10);
             memoryStream.Write(reader.ReadBytes(num), 0, num);
             obj2 = new Bitmap(Image.FromStream(memoryStream));
             memoryStream.Close();
         }
         else
         {
             if (!(pi.PropertyType == typeof(Stream)))
             {
                 throw new InvalidOperationException("Binary serializer do not support objects of type \"" + obj.GetType().ToString() + "\"");
             }
             int          num2          = reader.ReadInt32();
             MemoryStream memoryStream2 = new MemoryStream(num2);
             memoryStream2.Write(reader.ReadBytes(num2), 0, num2);
             obj2 = memoryStream2;
         }
         if (IsSerializableContent(pi.Name, obj))
         {
             pi.SetValue(obj, obj2, null);
             return(true);
         }
     }
     return(false);
 }