public object Read(List list) { object result = CreateObject(type); Properties props = new Properties(list); // iterate over all fields and properties foreach (FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type)) { LispChildAttribute ChildAttrib = (LispChildAttribute) field.GetCustomAttribute(typeof(LispChildAttribute)); if (ChildAttrib != null) { string Name = ChildAttrib.Name; if (field.Type == typeof(int)) { int val = 0; if (!props.Get(Name, ref val)) { if (!ChildAttrib.Optional) { LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp"); } } else { field.SetValue(result, val); } } else if (field.Type == typeof(string)) { string val = null; if (!props.Get(Name, ref val)) { if (!ChildAttrib.Optional) { LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp"); } } else { field.SetValue(result, val); } } else if (field.Type == typeof(float)) { float val = 0; if (!props.Get(Name, ref val)) { if (!ChildAttrib.Optional) { LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp"); } } else { field.SetValue(result, val); } } else if (field.Type.IsEnum) { Enum val = null; if (!props.Get(Name, ref val, field.Type)) { if (!ChildAttrib.Optional) { LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp"); } } else { field.SetValue(result, val); } } else if (field.Type == typeof(bool)) { bool val = false; if (!props.Get(Name, ref val)) { if (!ChildAttrib.Optional) { LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp"); } } else { field.SetValue(result, val); } } else if (field.Type == typeof(List <int>)) { List <int> val = new List <int>(); if (!props.GetIntList(Name, val)) { if (!ChildAttrib.Optional) { LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp"); } } else { field.SetValue(result, val); } } else { ILispSerializer serializer = LispSerializer.GetSerializer(field.Type); if (serializer == null) { throw new LispException("Type " + field.Type + " not supported for serialization"); } List val = null; if (!props.Get(Name, ref val)) { if (!ChildAttrib.Optional) { LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp"); } } else { object oval = serializer.Read(val); field.SetValue(result, oval); } } } foreach (LispChildsAttribute childsAttrib in field.GetCustomAttributes(typeof(LispChildsAttribute))) { object objectList = field.GetValue(result); Type ListType = field.Type; MethodInfo AddMethod = ListType.GetMethod( "Add", new Type[] { childsAttrib.ListType }, null); if (AddMethod == null) { throw new LispException("No Add method found for field " + field.Name); } ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type); if (serializer == null) { serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type); } foreach (List childList in props.GetList(childsAttrib.Name)) { object child = serializer.Read(childList); AddMethod.Invoke(objectList, new object[] { child }); } } } if (result is ICustomLispSerializer) { ICustomLispSerializer custom = (ICustomLispSerializer)result; custom.CustomLispRead(props); custom.FinishRead(); } return(result); }
public object Read(List list) { object result = CreateObject(type); Properties props = new Properties(list); // iterate over all fields and properties foreach (FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type)) { LispChildAttribute ChildAttrib = (LispChildAttribute) field.GetCustomAttribute(typeof(LispChildAttribute)); if (ChildAttrib != null) { string Name = ChildAttrib.Name; string altName = ChildAttrib.AlternativeName; if (field.Type == typeof(int)) { int val = 0; if (!(props.Get(Name, ref val) || (altName != null && props.Get(altName, ref val)))) { CheckRequired(ChildAttrib); } else { field.SetValue(result, val); } } else if (field.Type == typeof(string)) { string val = null; if (!props.Get(Name, ref val)) { CheckRequired(ChildAttrib); } else { field.SetValue(result, val); } } else if (field.Type == typeof(float)) { float val = 0; if (!(props.Get(Name, ref val) || (altName != null && props.Get(altName, ref val)))) { CheckRequired(ChildAttrib); } else { field.SetValue(result, val); } } else if (field.Type.IsEnum) { Enum val = null; if (!props.Get(Name, ref val, field.Type)) { CheckRequired(ChildAttrib); } else { field.SetValue(result, val); } } else if (field.Type == typeof(bool)) { bool val = false; if (!props.Get(Name, ref val)) { CheckRequired(ChildAttrib); } else { field.SetValue(result, val); } } else if (field.Type == typeof(List <int>)) { List <int> val = new List <int>(); if (!props.GetIntList(Name, val)) { CheckRequired(ChildAttrib); } else { field.SetValue(result, val); } } else if (field.Type == typeof(List <string>)) { List <string> val = new List <string>(); if (!props.GetStringList(Name, val)) { CheckRequired(ChildAttrib); } else { field.SetValue(result, val); } } else if (field.Type == typeof(Lisp.List)) { Lisp.List val; if (!props.GetLispList(Name, out val)) { CheckRequired(ChildAttrib); } else { field.SetValue(result, val); } } else { ILispSerializer serializer = LispSerializer.GetSerializer(field.Type); if (serializer == null) { throw new LispException("Type " + field.Type + " not supported for serialization"); } List val = null; if (!props.Get(Name, ref val)) { CheckRequired(ChildAttrib); } else { try { object oval = serializer.Read(val); field.SetValue(result, oval); } catch (LispException) { if (Name == "ambient-light" || Name == "music") { // ignore errors of these for backward compatibilty } else { throw; } } } } } foreach (LispChildsAttribute childsAttrib in field.GetCustomAttributes(typeof(LispChildsAttribute))) { object objectList = field.GetValue(result); Type ListType = field.Type; MethodInfo AddMethod = ListType.GetMethod( "Add", new Type[] { childsAttrib.ListType }, null); if (AddMethod == null) { throw new LispException("No Add method found for field " + field.Name); } ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type); if (serializer == null) { serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type); } foreach (List childList in props.GetList(childsAttrib.Name)) { object child = serializer.Read(childList); AddMethod.Invoke(objectList, new object[] { child }); } } } if (result is ICustomLispSerializer) { ICustomLispSerializer custom = (ICustomLispSerializer)result; custom.CustomLispRead(props); custom.FinishRead(); } return(result); }