static Action <IDataReader, object> CreateSetValueAction(PropertyInfo info, Type columnType, string columnName) { Type propType = info.PropertyType; Action <IDataReader, object> SetValueAction; if (propType.BaseType.Equals(typeof(System.Enum))) { SetValueAction = (IDataReader dr, object obj) => { if (IDataReaderExtensionHelper.IsNumeric(dr[columnName])) { info.SetValue(obj, System.Enum.ToObject(propType, Convert.ToInt32(dr[columnName])), null); } else { if (!dr.GetString(columnName).IsNullOrEmptyAfterTrim() && Enum.IsDefined(propType, dr[columnName])) { object value = Enum.Parse(propType, dr[columnName].ToString()); info.SetValue(obj, value, null); } } }; } else if (propType.Equals(columnType)) { SetValueAction = (IDataReader dr, object obj) => { info.SetValue(obj, dr[columnName], null); }; } else { //different types SetValueAction = (IDataReader dr, object obj) => { Type t = Nullable.GetUnderlyingType(propType) ?? propType; object safevalue = dr[columnName] == null ? null : Convert.ChangeType(dr[columnName], t); info.SetValue(obj, safevalue, null); }; } return((IDataReader dr, object obj) => { if (dr.HasColumn(columnName)) { if (Convert.IsDBNull(dr[columnName])) { info.SetValue(obj, null, null); // info.SetValue(obj, Null.GetNull(info), null); } else { SetValueAction.Invoke(dr, obj); } } }); }
public void SetValue(IBehaviorContext context, TValue value) { TValue oldValue = (TValue)context.VM.Kernel.GetValue(_property); var manager = UndoManager.GetManager(context.VM); if (manager != null) { var action = new SetValueAction <TValue>(context.VM, _property, oldValue); manager.PushAction(action); } this.SetValueNext(context, value); }
static Action <IDataReader, object> CreateSetValueAction(PropertyInfo info, Type columnType, string columnName) { Type propType = info.PropertyType; Action <IDataReader, object> SetValueAction; if (propType.BaseType.Equals(typeof(System.Enum))) { SetValueAction = (IDataReader dr, object obj) => { if (MappingHelper.IsNumeric(dr[columnName])) { info.SetValue(obj, System.Enum.ToObject(propType, Convert.ToInt32(dr[columnName])), null); } else { info.SetValue(obj, System.Enum.ToObject(propType, dr[columnName]), null); } }; } else if (propType.Equals(columnType)) { SetValueAction = (IDataReader dr, object obj) => { info.SetValue(obj, dr[columnName], null); }; } else { //different types SetValueAction = (IDataReader dr, object obj) => { info.SetValue(obj, Convert.ChangeType(dr[columnName], propType), null); }; } return((IDataReader dr, object obj) => { if (Convert.IsDBNull(dr[columnName])) { info.SetValue(obj, MappingNull.GetNull(info), null); } else { SetValueAction.Invoke(dr, obj); } }); }
/// <summary> /// Sets the value of the property with the given name for the given container. /// </summary> /// <param name="container">The container whose data will be set.</param> /// <param name="name">The property name to set.</param> /// <param name="value">The value to assign to the property.</param> /// <param name="changeTracker">The change tracker to increment if the value changes.</param> public static void SetValue <TContainer, TValue>(ref TContainer container, string name, TValue value, ref ChangeTracker changeTracker) { var propertyBag = PropertyBagResolver.Resolve <TContainer>(); if (null == propertyBag) { throw new Exception($"Failed to resolve property bag for ContainerType=[{typeof(TContainer)}]"); } var action = new SetValueAction <TContainer, TValue> { SrcValue = value }; if (!propertyBag.FindProperty(name, ref container, ref changeTracker, ref action)) { throw new Exception($"Failed to find property Name=[{name}] for ContainerType=[{typeof(TContainer)}]"); } if (action.Result == k_ResultErrorConvert) { throw new Exception($"Failed assign ValueType=[{typeof(TValue)}] to property Name=[{name}] for ContainerType=[{typeof(TContainer)}]"); } }