void PopulateStateFromProperties(TaskEntity target, Task source) { var sourceType = source.GetType(); var taskType = typeof(Task); #if(NETFX_CORE) var taskProperties = taskType.GetTypeInfo().DeclaredProperties; var declaringTypeProperties = sourceType.GetTypeInfo().DeclaredProperties.Where(p => p.DeclaringType == sourceType); #else var taskProperties = taskType.GetProperties(); var declaringTypeProperties = sourceType.GetProperties().Where(p => p.DeclaringType == sourceType); #endif var sourceProperties = declaringTypeProperties.Where(p=>!taskProperties.Any(pp=>pp.Name == p.Name)); var sourcePropertiesDictionary = sourceProperties.ToDictionary(p => p.Name, p => p.GetValue(source, null).ToString()); sourcePropertiesDictionary.ForEach(target.State.Add); }
TaskEntity ToTaskEntity(Task task, TaskEntity taskEntity = null) { if( taskEntity == null ) taskEntity = new TaskEntity(); taskEntity.Id = task.Id; taskEntity.CurrentOperation = task.CurrentOperation; taskEntity.Type = task.GetType(); PopulateStateFromProperties(taskEntity, task); return taskEntity; }
#pragma warning restore 1591 // Xml Comments Task ToTask(TaskEntity taskEntity) { var task = _container.Get(taskEntity.Type) as Task; task.Id = taskEntity.Id; task.CurrentOperation = taskEntity.CurrentOperation; PopulatePropertiesFromState(task, taskEntity); return task; }
void PopulatePropertiesFromState(Task target, TaskEntity source) { var targetType = target.GetType(); foreach (var key in source.State.Keys) { #if(NETFX_CORE) var property = targetType.GetTypeInfo().GetDeclaredProperty(key); #else var property = targetType.GetProperty(key); #endif if (property != null) { var value = Convert.ChangeType(source.State[key], property.PropertyType, null); property.SetValue(target, value, null); } } }
void PopulatePropertiesFromState(Task target, TaskEntity source) { var targetType = target.GetType(); foreach (var key in source.State.Keys) { var property = targetType.GetProperty(key); if (property != null) { var value = Convert.ChangeType(source.State[key], property.PropertyType, null); property.SetValue(target, value, null); } } }