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 = SerializerBase.FontFromString(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 { PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(obj)[attrName]; bool flag = false; if (propertyDescriptor != null) { try { TypeConverterAttribute typeConverterAttribute = (TypeConverterAttribute)propertyDescriptor.Attributes[typeof(TypeConverterAttribute)]; if (typeConverterAttribute != null && typeConverterAttribute.ConverterTypeName.Length > 0) { 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(SR.ExceptionChartSerializerPropertyNameUnknown(attrName, obj.GetType().ToString())); } }
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(byte)) { obj2 = reader.ReadByte(); } else if (pi.PropertyType == typeof(Font)) { obj2 = SerializerBase.FontFromString(reader.ReadString()); } else if (pi.PropertyType == typeof(Color)) { obj2 = Color.FromArgb(reader.ReadInt32()); } else if (pi.PropertyType == typeof(DateTime)) { obj2 = new DateTime(reader.ReadInt64()); } else if (pi.PropertyType == typeof(Size)) { obj2 = new Size(reader.ReadInt32(), reader.ReadInt32()); } else if (pi.PropertyType == typeof(Margins)) { obj2 = new Margins(reader.ReadInt32(), reader.ReadInt32(), 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(Color[])) { Color[] array2 = new Color[reader.ReadInt32()]; for (int j = 0; j < array2.Length; j++) { array2[j] = Color.FromArgb(reader.ReadInt32()); } obj2 = array2; } else { if (!(pi.PropertyType == typeof(Image))) { throw new InvalidOperationException(SR.ExceptionChartSerializerBinaryTypeUnsupported(obj.GetType().ToString())); } 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(); } if (IsSerializableContent(pi.Name, obj)) { pi.SetValue(obj, obj2, null); return(true); } } return(false); }