示例#1
0
        private void SetXmlValue(object obj, string attrName, string attrValue)
        {
            PropertyInfo property = obj.GetType().GetProperty(attrName);

            if (property != null)
            {
                object value = attrValue;
                if (property.PropertyType == typeof(string))
                {
                    value = attrValue;
                }
                else if (property.PropertyType == typeof(Font))
                {
                    value = (Font)SerializerBase.fontConverter.ConvertFromString(null, CultureInfo.InvariantCulture, attrValue);
                }
                else if (property.PropertyType == typeof(CultureInfo))
                {
                    value = CultureInfo.GetCultureInfoByIetfLanguageTag(attrValue);
                }
                else if (property.PropertyType == typeof(Color))
                {
                    value = (Color)SerializerBase.colorConverter.ConvertFromString(null, CultureInfo.InvariantCulture, attrValue);
                }
                else if (property.PropertyType == typeof(Image))
                {
                    value = SerializerBase.ImageFromString(attrValue);
                }
                else if (property.PropertyType == typeof(ShapeData))
                {
                    value = ShapeData.ShapeDataFromString(attrValue);
                }
                else if (property.PropertyType == typeof(Stream))
                {
                    value = SerializerBase.StreamFromString(attrValue);
                }
                else
                {
                    PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(obj)[attrName];
                    bool flag = false;
                    if (propertyDescriptor != null)
                    {
                        try
                        {
                            TypeConverterAttribute typeConverterAttribute = (TypeConverterAttribute)propertyDescriptor.Attributes[typeof(TypeConverterAttribute)];
                            if (typeConverterAttribute != null && !string.IsNullOrEmpty(typeConverterAttribute.ConverterTypeName))
                            {
                                Assembly      assembly      = Assembly.GetAssembly(GetType());
                                string[]      array         = typeConverterAttribute.ConverterTypeName.Split(',');
                                TypeConverter typeConverter = (TypeConverter)assembly.CreateInstance(array[0]);
                                if (typeConverter != null && typeConverter.CanConvertFrom(typeof(string)))
                                {
                                    value = typeConverter.ConvertFromString(null, CultureInfo.InvariantCulture, attrValue);
                                    flag  = true;
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }
                        if (!flag && propertyDescriptor.Converter != null && propertyDescriptor.Converter.CanConvertFrom(typeof(string)))
                        {
                            value = propertyDescriptor.Converter.ConvertFromString(null, CultureInfo.InvariantCulture, attrValue);
                        }
                    }
                }
                property.SetValue(obj, value, null);
            }
            else if (!base.IgnoreUnknownAttributes)
            {
                throw new InvalidOperationException("Can't deserialize property. Unknown property name \"" + attrName + "\" in object " + obj.GetType().ToString());
            }
        }
示例#2
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() + "\"");
 }