public override object ReadJson(JsonReader Reader, Type ObjectType, object ExistingValue, JsonSerializer Serializer) { int Id = (int)(long)Reader.Value; if (Id != 0) { CsvData CsvData = CsvFiles.GetWithGlobalId(Id); if (ObjectType == typeof(CsvData) || CsvData.GetType() == ObjectType) { return(CsvData); } Logging.Error(this.GetType(), "CsvData.GetType() != ObjectType. Data:" + CsvData.GetType() + ", objectType:" + ObjectType + "."); } return(null); }
/// <summary> /// Reads the data. /// </summary> /// <param name="Data">The data.</param> /// <exception cref="System.Exception"></exception> public void LoadData(CsvData Data) { foreach (PropertyInfo Property in Data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { if (Property.CanRead && Property.CanWrite) { if (Property.PropertyType.IsArray) { Type ElementType = Property.PropertyType.GetElementType(); if (ElementType == typeof(bool)) { Property.SetValue(Data, this.LoadBoolArray(Property.Name)); } else if (ElementType == typeof(int)) { Property.SetValue(Data, this.LoadIntArray(Property.Name)); } else if (ElementType == typeof(string)) { Property.SetValue(Data, this.LoadStringArray(Property.Name)); } else { throw new Exception(ElementType + "[] is not a valid array."); } } else if (Property.PropertyType.IsGenericType) { if (Property.PropertyType == typeof(List <>)) { Type ListType = typeof(List <>); Type[] Generic = Property.PropertyType.GetGenericArguments(); Type ConcreteType = ListType.MakeGenericType(Generic); object NewList = Activator.CreateInstance(ConcreteType); MethodInfo Add = ConcreteType.GetMethod("Add"); string IndexerName = ((DefaultMemberAttribute)NewList.GetType().GetCustomAttributes(typeof(DefaultMemberAttribute), true)[0]).MemberName; PropertyInfo IndexProperty = NewList.GetType().GetProperty(IndexerName); for (int i = this.Offset; i < this.Offset + this.GetSize(Property.Name); i++) { string Value = this.GetValue(Property.Name, i - this.Offset); if (Value == string.Empty && i != this.Offset) { Value = IndexProperty.GetValue(NewList, new object[] { i - this.Offset - 1 }).ToString(); } if (string.IsNullOrEmpty(Value)) { object Object = Generic[0].IsValueType ? Activator.CreateInstance(Generic[0]) : string.Empty; Add.Invoke(NewList, new[] { Object }); } else { Add.Invoke(NewList, new[] { Convert.ChangeType(Value, Generic[0]) }); } } Property.SetValue(Data, NewList); } else if (Property.PropertyType == typeof(CsvData) || Property.PropertyType.BaseType == typeof(CsvData)) { this.LoadData((CsvData)Property.GetValue(Property)); } } else { string Value = this.GetValue(Property.Name, 0); if (!string.IsNullOrEmpty(Value)) { Property.SetValue(Data, Convert.ChangeType(Value, Property.PropertyType)); } } } } }