private static void xmlReadListOfObjects(object ParentObj, XmlReader xdr, IObservableList observableList) { // read list of object into the list, add one by one, like activities, actions etc. //TODO: Think/check if we want to make all observ as lazy load if (LazyLoadAttr.Contains(xdr.Name)) // if (FastLoad) // && xdr.Name == nameof(BusinessFlow.Activities) || xdr.Name != nameof(Activity.Acts)) { // We can save line/col and reload later when needed string s = xdr.ReadOuterXml(); // .ReadInnerXml(); // .Read(); observableList.DoLazyLoadItem(s); return; } if (observableList.GetType() == typeof(ObservableList <RepositoryItemKey>)) { xdr.Read(); while (xdr.NodeType != XmlNodeType.EndElement) { string RIKey = xdr.ReadElementContentAsString(); if (RIKey != null) { RepositoryItemKey repositoryItemKey = new RepositoryItemKey(); repositoryItemKey.Key = RIKey; observableList.Add(repositoryItemKey); } else { return; } } xdr.ReadEndElement(); } else { xdr.Read(); while (xdr.NodeType != XmlNodeType.EndElement) { object item = xmlReadObject(ParentObj, xdr); if (item != null) { observableList.Add(item); } else { return; } } xdr.ReadEndElement(); } }
private static void SetObjAttrValue(object obj, PropertyInfo propertyInfo, string sValue) { try { System.TypeCode typeCode = Type.GetTypeCode(propertyInfo.PropertyType); switch (typeCode) { case TypeCode.String: propertyInfo.SetValue(obj, sValue); break; case TypeCode.Int32: if (propertyInfo.PropertyType.IsEnum) { object o = Enum.Parse(propertyInfo.PropertyType, sValue); if (o != null) { propertyInfo.SetValue(obj, o); } else { throw new Exception("Cannot convert Enum - " + sValue); } } else { propertyInfo.SetValue(obj, Int32.Parse(sValue)); } break; case TypeCode.Int64: propertyInfo.SetValue(obj, Int64.Parse(sValue)); break; case TypeCode.Double: propertyInfo.SetValue(obj, double.Parse(sValue)); break; case TypeCode.Decimal: propertyInfo.SetValue(obj, decimal.Parse(sValue)); break; case TypeCode.DateTime: propertyInfo.SetValue(obj, DateTime.Parse(sValue)); break; case TypeCode.Boolean: if (sValue.ToUpper() == "FALSE") { propertyInfo.SetValue(obj, false); return; } if (sValue.ToUpper() == "TRUE") { propertyInfo.SetValue(obj, true); return; } break; case TypeCode.Object: if (propertyInfo.PropertyType == typeof(System.Guid)) { if (sValue != "00000000-0000-0000-0000-00000000") { propertyInfo.SetValue(obj, new Guid(sValue)); } } else if (propertyInfo.PropertyType == typeof(RepositoryItemKey)) { RepositoryItemKey repositoryItemKey = new RepositoryItemKey(); repositoryItemKey.Key = sValue; propertyInfo.SetValue(obj, repositoryItemKey); } else { //check if this is nullable enum like: Activity Status? if (Nullable.GetUnderlyingType(propertyInfo.PropertyType).IsEnum) { object o = Enum.Parse(Nullable.GetUnderlyingType(propertyInfo.PropertyType), sValue); if (o != null) { propertyInfo.SetValue(obj, o); } else { throw new Exception("Cannot convert Enum - " + sValue); } } else // handle long? = int64 nullable - used in elapsed if (Type.GetTypeCode(Nullable.GetUnderlyingType(propertyInfo.PropertyType)) == TypeCode.Int64) { if (sValue != null) { propertyInfo.SetValue(obj, Int64.Parse(sValue)); } else { throw new Exception("Cannot convert Nullable Int64 - " + sValue); } } else if (Type.GetTypeCode(Nullable.GetUnderlyingType(propertyInfo.PropertyType)) == TypeCode.Int32) { if (sValue != null) { propertyInfo.SetValue(obj, Int32.Parse(sValue)); } else { throw new Exception("Cannot convert Nullable Int32 - " + sValue); } } else if (Type.GetTypeCode(Nullable.GetUnderlyingType(propertyInfo.PropertyType)) == TypeCode.Double) { if (sValue != null) { propertyInfo.SetValue(obj, Double.Parse(sValue)); } else { throw new Exception("Cannot convert Nullable Double - " + sValue); } } else { throw new Exception("Serializer - Err set value, Unknown type - " + propertyInfo.PropertyType.ToString() + " Value: " + sValue); } } break; default: throw new Exception("Serializer - Err set value, Unknow type - " + propertyInfo.PropertyType.ToString() + " Value: " + sValue); } //TODO: all other types } catch { string err; if (propertyInfo != null) { err = "Obj=" + obj + ", Property=" + propertyInfo.Name + ", Value=" + sValue.ToString(); } else { err = "Property Not found: Obj=" + obj + " Value=" + sValue.ToString(); } throw new Exception(err); } }