public static ICollection <IClientEntity> GetImmidiateChildrenAndClear(IClientEntity clientEntity) { var childList = new List <IClientEntity>(); Type objectType = clientEntity.GetType(); PropertyInfo[] properties = objectType.GetProperties(); foreach (PropertyInfo propertyInfo in properties) { try { Object value = propertyInfo.GetValue(clientEntity, null); if (value != null) { if (value is IList) { var enumerable = (IList)value; foreach (Object o in enumerable) { if (o is IClientEntity) { childList.Add((IClientEntity)o); } } enumerable.Clear(); } else if (value is IClientEntity) { childList.Add((IClientEntity)value); propertyInfo.SetValue(clientEntity, null, null); } } } catch (Exception e) { LogManager.GetLogger(typeof(StatusManager)).Fatal( "Exception occured while trying to retrieve child objects", e); } } return(childList); }
public static void SetStatus(IClientEntity clientEntity, EntityStatus status) { if (clientEntity == null) { return; } clientEntity.Status = status; Type objectType = clientEntity.GetType(); PropertyInfo[] properties = objectType.GetProperties(); foreach (PropertyInfo propertyInfo in properties) { try { Object value = propertyInfo.GetValue(clientEntity, null); if (value != null) { if (value is ICollection) { var enumerable = (ICollection)value; foreach (Object o in enumerable) { if (o is IClientEntity) { SetStatus((IClientEntity)o, status); } } } else if (value is IClientEntity) { SetStatus((IClientEntity)value, status); } } } catch (Exception e) { LogManager.GetLogger(typeof(StatusManager)).Fatal( "Exception occured while trying to update status", e); } } }