/// <summary> /// Converts a DataSource to an Object /// </summary> public static object ToObject(this DataNode node, Type objectType) { if (node == null) { return(null); } var info = objectType.GetTypeInfo(); var fields = info.DeclaredFields.Where(f => f.IsPublic); var result = Activator.CreateInstance(objectType); // box result otherwise structs values wont update object obj = result; foreach (var field in fields) { if (!node.HasNode(field.Name)) { continue; } var fieldType = field.FieldType; if (fieldType.IsPrimitive()) { var str = node.GetString(field.Name); #region TYPES LIST if (fieldType == typeof(string)) { field.SetValue(obj, str); } else if (fieldType == typeof(byte)) { byte val; byte.TryParse(str, out val); field.SetValue(obj, val); } else if (fieldType == typeof(sbyte)) { sbyte val; sbyte.TryParse(str, out val); field.SetValue(obj, val); } else if (fieldType == typeof(short)) { short val; short.TryParse(str, out val); field.SetValue(obj, val); } else if (fieldType == typeof(ushort)) { ushort val; ushort.TryParse(str, out val); field.SetValue(obj, val); } else if (fieldType == typeof(int)) { int val; int.TryParse(str, out val); field.SetValue(obj, val); } else if (fieldType == typeof(uint)) { uint val; uint.TryParse(str, out val); field.SetValue(obj, val); } else if (fieldType == typeof(long)) { long val; long.TryParse(str, out val); field.SetValue(obj, val); } else if (fieldType == typeof(ulong)) { ulong val; ulong.TryParse(str, out val); field.SetValue(obj, val); } else if (fieldType == typeof(float)) { float val; float.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture.NumberFormat, out val); field.SetValue(obj, val); } else if (fieldType == typeof(double)) { double val; double.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture.NumberFormat, out val); field.SetValue(obj, val); } else if (fieldType == typeof(decimal)) { decimal val; decimal.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture.NumberFormat, out val); field.SetValue(obj, val); } else if (fieldType == typeof(bool)) { bool val; bool.TryParse(str, out val); field.SetValue(obj, val); } else { throw new Exception("Cannot unserialize field of type " + objectType.Name); } #endregion } else { var valNode = node.GetNode(field.Name); object val = valNode.ToObject(fieldType); field.SetValue(obj, val); } } return(Convert.ChangeType(obj, objectType)); }
/// <summary> /// Converts an object to a DataSource /// </summary> public static DataNode ToDataNode(this object obj, string name = null, bool isArrayElement = false) { if (obj == null) { return(null); } Type type = obj.GetType(); if (type.IsArray) { return(FromArray(obj)); } else if (IsPrimitive(type)) { throw new Exception("Can't convert primitive type to DataNode"); } TypeInfo info = null; var fields = Enumerable.Empty <FieldInfo>(); Type currentClass = type; do { var currentInfo = currentClass.GetTypeInfo(); if (currentClass == type) { info = currentInfo; } var temp = currentInfo.DeclaredFields.Where(f => f.IsPublic); var fieldArray = temp.ToArray(); fields = temp.Concat(fields); currentClass = currentInfo.BaseType; if (currentClass == typeof(object)) { break; } } while (true); if (name == null && !isArrayElement) { name = type.Name.ToLower(); } var result = DataNode.CreateObject(name); foreach (var field in fields) { var val = field.GetValue(obj); var fieldName = field.Name.ToLower(); var fieldTypeInfo = field.FieldType.GetTypeInfo(); if (field.FieldType.IsPrimitive() || fieldTypeInfo.IsEnum) { result.AddField(fieldName, val); } else if (fieldTypeInfo.IsArray) { var arrayNode = FromArray(val, fieldName); result.AddNode(arrayNode); } else if (val != null) { var node = val.ToDataNode(fieldName); result.AddNode(node); } else { result.AddField(fieldName, null); } } return(result); }