/// <summary> /// 将控制器中的数据与指定对象中的数据进行比较,如果数据发生改变,返回true,否则返回false /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value">需要进行比较的对象</param> /// <returns></returns> public bool CheckChanged <T>(T value) { foreach (var property in typeof(T).GetProperties().Where(p => p.CanRead)) { foreach (var dataBindingItem in dataBindingItems.Where(item => string.Compare(item.DataMember, property.Name, true) == 0)) { var controlValue = ControlValueReader.Read(dataBindingItem.Control, property.PropertyType); var propertyValue = property.GetValue(value, null); if (controlValue != null && !controlValue.Equals(propertyValue)) { return(true); } } } return(false); }
/// <summary> /// 从控件中获取指定对象的属性值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> public void GetValue <T>(T value) { foreach (var property in typeof(T).GetProperties().Where(p => p.CanWrite)) { var dataBindingItem = dataBindingItems.FirstOrDefault(item => string.Compare(item.DataMember, property.Name, true) == 0); if (dataBindingItem != null) { var returnType = property.PropertyType; if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Nullable <>)) { returnType = returnType.GetGenericArguments()[0]; //处理可空类型 } var controlValue = ControlValueReader.Read(dataBindingItem.Control, returnType); //将空字符串转换成null if (this.ConvertEmptyStringToNull && controlValue != null && property.PropertyType == typeof(string) && controlValue.ToString() == string.Empty) { controlValue = null; } property.SetValue(value, controlValue, null); } } }