public virtual void CopyPropertiesFrom(object model) { //Properties holen if (!(this.GetType().GetTypeInfo().IsAssignableFrom(model.GetType().GetTypeInfo()))) { CopyPropertiesFromDifferentType(model); return; } var props = model.GetType().GetRuntimeProperties(); //Wir verwenden hier GetRuntimeProperties anstelle von GetProperties. Das wird für //.NET 4.5 beispielsweise per Projection auf GetProperties gemapped (öffentliche Member, aber die reichen), //funktioniert aber auch in WinRt und Windows Phone. foreach (var propItem in props) { try { object value = propItem.GetValue(model, null); propItem.SetValue(this, value, null); } catch (Exception ex) { CopyPropertiesException copPropException = new CopyPropertiesException("Model property '" + propItem.Name + "' could not be copied to ViewModel property '" + propItem.Name + "'." + Environment.NewLine + "Reason: " + ex.Message); if (IsDebugMode) { Debug.WriteLine("Model property '" + propItem.Name + "' could not be copied to ViewModel property '" + propItem.Name + "'." + Environment.NewLine + "Reason: " + ex.Message); } } } }
/// <summary> /// Kopiert die Inhalte der Eigenschaften von einer anderen Instanz dieser Klasse in die aktuelle. /// </summary> /// <param name="model"></param> /// <remarks></remarks> public virtual void CopyPropertiesTo(object model) { //Properties holen if (!(this.GetType().GetTypeInfo().IsAssignableFrom(model.GetType().GetTypeInfo()))) { CopyPropertiesToDifferentType(model); return; } var props = model.GetType().GetRuntimeProperties(); foreach (var propItem in props) { try { object value = propItem.GetValue(this, null); propItem.SetValue(model, value, null); } catch (Exception ex) { CopyPropertiesException copPropException = new CopyPropertiesException("Model property '" + propItem.Name + "' could not be copied to ViewModel property '" + propItem.Name + "'." + Environment.NewLine + "Reason: " + ex.Message); if (IsDebugMode) { Debug.WriteLine("Model property '" + propItem.Name + "' could not be copied to ViewModel property '" + propItem.Name + "'." + Environment.NewLine + "Reason: " + ex.Message); } } } }
private void CopyPropertiesFromDifferentType(object model) { //Liste erstellen, mit ViewModel- und Model-PropertyInfo-Objekten //Create a list with ViewModel and Model PropertyInfo objects. //INSTANT C# WARNING: Every field in a C# anonymous type initializer is immutable: //ORIGINAL LINE: For Each propToPropItem In From propItem In GetType.GetRuntimeProperties Where propItem.GetCustomAttribute(Of ModelPropertyIgnoreAttribute)() Is Nothing // Select New With { .ViewModelProperty = propItem, .ModelProperty = model.GetType.GetRuntimeProperty(If(propItem.GetCustomAttribute(Of ModelPropertyNameAttribute)() Is Nothing, // propItem.Name, propItem.GetCustomAttribute(Of ModelPropertyNameAttribute).PropertyName))} foreach (var propToPropItem in from propItem in GetType().GetRuntimeProperties() select new { ViewModelProperty = propItem, ModelProperty = model.GetType().GetRuntimeProperty(((propItem.GetCustomAttribute <ModelPropertyNameAttribute>() == null) ? propItem.Name : propItem.GetCustomAttribute <ModelPropertyNameAttribute>().PropertyName)) }) { //Falls es die entsprechende Eigenschaft im Model nicht gibt, dann überspringen. //Skip the ViewModel property, if a corresponding property does not exist in the model. if (propToPropItem.ModelProperty == null) { continue; } //Liste Element für Element abarbeiten, und Werte aus der einen Eigenschaft in die andere Eigenschaft übernehmen. //Interate through list item by item and copy the property values from one property to the other. try { object value = null; if (typeof(IDiscoverableValue).GetTypeInfo().IsAssignableFrom(propToPropItem.ModelProperty.PropertyType.GetTypeInfo())) { var source = (IDiscoverableValue)propToPropItem.ModelProperty.GetValue(model, null); value = source.GetValue(); } else { value = propToPropItem.ModelProperty.GetValue(model, null); } if (typeof(IDiscoverableValue).GetTypeInfo().IsAssignableFrom(propToPropItem.ViewModelProperty.PropertyType.GetTypeInfo())) { var target = (IDiscoverableValue)propToPropItem.ViewModelProperty.GetValue(this, null); target.SetValue(value); } else { propToPropItem.ViewModelProperty.SetValue(this, value, null); } } catch (Exception ex) { CopyPropertiesException copPropException = new CopyPropertiesException("Model property '" + propToPropItem.ModelProperty.Name + "' could not be copied to ViewModel property '" + propToPropItem.ModelProperty.Name + "'." + Environment.NewLine + "Reason: " + ex.Message); Debug.WriteLine("Model property '" + propToPropItem.ModelProperty.Name + "' could not be copied to ViewModel property '" + propToPropItem.ModelProperty.Name + "'." + Environment.NewLine + "Reason: " + ex.Message); } } }
private void CopyPropertiesToDifferentType(object model) { //Liste erstellen, mit ViewModel- und Model-PropertyInfo-Objekten //Create a list with ViewModel and Model PropertyInfo objects. foreach (var propToPropItem in from propItem in GetType().GetRuntimeProperties() where (propItem.GetCustomAttribute <ModelPropertyIgnoreAttribute>() == null) select new { ViewModelProperty = propItem, ModelProperty = model.GetType().GetRuntimeProperty(((propItem.GetCustomAttribute <ModelPropertyNameAttribute>() == null) ? propItem.Name : propItem.GetCustomAttribute <ModelPropertyNameAttribute>().PropertyName)) }) { //Falls es die entsprechende Eigenschaft im Model nicht gibt, dann überspringen. //Skip the ViewModel property, if a corresponding property does not exist in the model. if (propToPropItem.ModelProperty == null) { continue; } //Liste Element für Element abarbeiten, und Werte aus der einen Eigenschaft in die andere Eigenschaft übernehmen. //Interate through list item by item and copy the property values from one property to the other. try { object value = null; if (typeof(IDiscoverableValue).GetTypeInfo().IsAssignableFrom(propToPropItem.ViewModelProperty.PropertyType.GetTypeInfo())) { if (propToPropItem.ViewModelProperty.CanRead) { var source = (IDiscoverableValue)propToPropItem.ViewModelProperty.GetValue(this, null); value = source.GetValue(); } } else { if (propToPropItem.ViewModelProperty.CanRead) { value = propToPropItem.ViewModelProperty.GetValue(this, null); } } if (typeof(IDiscoverableValue).GetTypeInfo().IsAssignableFrom(propToPropItem.ModelProperty.PropertyType.GetTypeInfo())) { if (propToPropItem.ModelProperty.CanWrite) { var target = (IDiscoverableValue)propToPropItem.ModelProperty.GetValue(model, null); target.SetValue(value); } } else { if (propToPropItem.ModelProperty.CanWrite) { propToPropItem.ModelProperty.SetValue(model, value, null); } } } catch (Exception ex) { CopyPropertiesException copPropException = new CopyPropertiesException("ViewModel property '" + propToPropItem.ViewModelProperty.Name + "' could not be copied to Model property '" + propToPropItem.ModelProperty.Name + "'." + Environment.NewLine + "Reason: " + ex.Message); if (IsDebugMode) { Debug.WriteLine("ViewModel property '" + propToPropItem.ViewModelProperty.Name + "' could not be copied to Model property '" + propToPropItem.ModelProperty.Name + "'." + Environment.NewLine + "Reason: " + ex.Message); } } } }