public ViewmodelBase(object model) { _properties = new Dictionary <string, object>(); Model = model; ModelType = model.GetType(); foreach (var property in ModelType.GetProperties()) { if (property.CanRead) { object value = property.GetValue(model, null); Type propertyType = property.PropertyType; if (propertyType.IsClass && !(propertyType == typeof(String))) { var childVM = new ViewmodelBase(value); _properties[property.Name] = childVM; childVM.Modified += _childVM_Modified; } else { _properties[property.Name] = value; } } } _isModified = false; }
public virtual void SaveToModel() { foreach (var property in _properties) { var propertyInfo = ModelType.GetProperty(property.Key); if (propertyInfo.CanWrite) { object propertyValue = null; ViewmodelBase propertyVM = property.Value as ViewmodelBase; if (propertyVM != null) { propertyVM.SaveToModel(); propertyValue = propertyVM.Model; } else { propertyValue = property.Value; } propertyInfo.SetValue(Model, propertyValue, null); } } }