static dynamic ToDynamic(Task task) { dynamic dynamicTask = new ExpandoObject(); dynamicTask.Type = task.GetType().Name; dynamicTask.Id = task.Id.Value; dynamicTask.CurrentOperation = task.CurrentOperation; PopulateStateFromProperties(dynamicTask, task); return dynamicTask; }
static void PopulateStateFromProperties(ExpandoObject target, Task source) { var sourceType = source.GetType(); var taskType = typeof(Task); var taskProperties = taskType.GetProperties(); var declaringTypeProperties = sourceType.GetProperties().Where(p => p.DeclaringType == sourceType); var sourceProperties = declaringTypeProperties.Where(p => !taskProperties.Any(pp => pp.Name == p.Name)); var dictionary = sourceProperties.ToDictionary(p => p.Name, p => p.GetValue(source, null).ToString()); foreach (var key in dictionary.Keys) ((IDictionary<string, object>)target)[key] = dictionary[key]; }
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; }
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); } } }