public void UtilsTest_EnumViewModel_Parse_Null() { var value = EnumViewModel.Parse(null, typeof(FavorateTypeWithLabel)); Assert.IsNull(value); value = EnumViewModel.Parse(string.Empty, typeof(FavorateTypeWithLabel)); Assert.IsNull(value); }
/// <summary> /// 获取动态属性的值。 /// </summary> /// <param name="dynamicProperty">对应的动态属性的名称。</param> /// <param name="desiredType">需要转换的类型。</param> /// <returns></returns> public object GetDynamicProperty(string dynamicProperty, Type desiredType = null) { var result = this[dynamicProperty]; if (desiredType != null && result != null) { if (desiredType.IsEnum) { result = EnumViewModel.Parse(result as string, desiredType); } result = TypeHelper.CoerceValue(desiredType, result); } return(result); }
internal static object ToServerValue(Type serverType, object value) { if (serverType.IsEnum) { value = EnumViewModel.Parse(value as string, serverType); } else if (serverType == typeof(Nullable <int>)) { var intValue = Convert.ToInt32(value); if (intValue == 0) { value = null; } else if (!(value is int)) { value = intValue; } } else if (serverType == typeof(Nullable <Guid>)) { if (value != null) { var guidValue = Guid.Parse(value.ToString()); if (guidValue == Guid.Empty) { value = null; } } } else if (serverType == typeof(Guid)) { if (value != null) { value = Guid.Parse(value.ToString()); } else { value = Guid.Empty; } } return(value); }
/// <summary> /// 获取指定名称的动态属性值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dynamicProperty">对应的动态属性的名称。</param> /// <param name="defaultValue">如果属性还没有值,则返回这个默认值。</param> /// <returns></returns> public T GetDynamicPropertyOrDefault <T>(string dynamicProperty, T defaultValue = default(T)) { object result; if (_dynamics != null) { if (_dynamics.TryGetValue(dynamicProperty, out result)) { var desiredType = typeof(T); if (desiredType.IsEnum) { result = EnumViewModel.Parse(result as string, desiredType); } return((T)TypeHelper.CoerceValue(desiredType, result)); } } return(defaultValue); }
public override void SetValue(object component, object value) { var mp = this.Property; var entity = component as Entity; if (mp is IRefIdProperty) { entity.SetRefNullableId(mp as IRefIdProperty, value, ManagedPropertyChangedSource.FromUIOperating); return; } //枚举值在为界面返回值时,应该返回 Label if (value != null) { var enumType = TypeHelper.IgnoreNullable(mp.PropertyType); if (enumType.IsEnum) { value = EnumViewModel.Parse((value as string).TranslateReverse(), enumType); } } //使用 PropertyDescriptor 来进行操作属性时,都是作为 UI 层操作。 entity.SetProperty(mp, value, ManagedPropertyChangedSource.FromUIOperating); }
private IConstraint CreateColumnConstraint(string comparison, string value) { #region 转换操作符 var op = PropertyOperator.Equal; switch (comparison.ToLower()) { case "eq": op = PropertyOperator.Equal; break; case "ne": op = PropertyOperator.NotEqual; break; case "gt": op = PropertyOperator.Greater; break; case "ge": op = PropertyOperator.GreaterEqual; break; case "lt": op = PropertyOperator.Less; break; case "le": op = PropertyOperator.LessEqual; break; case "contains": op = PropertyOperator.Contains; break; case "startswith": op = PropertyOperator.StartsWith; break; case "endswith": op = PropertyOperator.EndsWith; break; case "notcontains": op = PropertyOperator.NotContains; break; case "notstartswith": op = PropertyOperator.NotStartsWith; break; case "notendswith": op = PropertyOperator.NotEndsWith; break; default: throw new NotSupportedException("不支持这个操作符:" + comparison + "。"); } #endregion #region 把表达式中的值转换为列的类型对应的值。(同时,兼容处理枚举的 Label 值。) object columnValue = null; var propertyType = _column.Property.PropertyType; var innerType = TypeHelper.IgnoreNullable(propertyType); if (innerType.IsEnum) { columnValue = EnumViewModel.Parse(value, innerType); } else { columnValue = TypeHelper.CoerceValue(_column.Property.PropertyType, value); } #endregion return(_f.Constraint(_column, op, columnValue)); }
public void UtilsTest_EnumViewModel_Parse_Value() { var value = EnumViewModel.Parse("1", typeof(FavorateTypeWithLabel)); Assert.AreEqual(FavorateTypeWithLabel.B, value); }
/// <summary> /// 遍历 JSON 对象的属性,并使用托管属性 API 来设置一般属性的值。 /// </summary> /// <param name="jObject"></param> /// <param name="entity"></param> private void DeserializeProperties(JObject jObject, Entity entity) { var properties = entity.PropertiesContainer.GetAvailableProperties(); foreach (var propertyValue in jObject) { var propertyName = propertyValue.Key; var jValue = propertyValue.Value; var mp = properties.Find(propertyName, true) as IProperty; if (mp != null) { //只读属性不需要反序列化。 if (mp.IsReadOnly) { continue; } //幽灵属性也不需要处理。 if (mp == EntityConvention.Property_IsPhantom) { continue; } if (mp is IListProperty) { DeserializeList(entity, mp as IListProperty, jValue as JArray); } else if (mp is IRefEntityProperty) { //一般引用属性不支持反序列化。 } else if (mp.IsRedundant) { //冗余属性不支持反序列化。 //冗余属性在反序列化时,如果值是错误的(例如在客户端刚创建的实体),而且它在对应的引用属性之后进行反序列化的话,就会导致值出错。 //另外,由于引用属性在反序列化时,都会计算相应的冗余属性,所以这里不再需要对冗余属性进行反序列化。 } //一般属性。 else { #region 处理不同类型的 value object value = null; if (jValue is JArray) { #region 对于数组的泛型列表类型,需要进行特殊的处理。 var jArray = jValue as JArray; var propertyType = mp.PropertyType; if (propertyType.IsArray)//string[] { var elementType = propertyType.GetElementType(); var array = Array.CreateInstance(elementType, jArray.Count); for (int i = 0, c = jArray.Count; i < c; i++) { var itemTyped = TypeHelper.CoerceValue(elementType, jArray[i]); array.SetValue(itemTyped, i); } value = array; } else if (TypeHelper.IsGenericType(propertyType, typeof(List <>)))//List<string> { var elementType = propertyType.GetGenericArguments()[0]; var list = Activator.CreateInstance(propertyType) as IList; for (int i = 0, c = jArray.Count; i < c; i++) { var itemTyped = TypeHelper.CoerceValue(elementType, jArray[i]); list.Add(itemTyped); } value = list; } else { //如果不是数组类型或者泛型列表类型的属性,则不支持反序列化。 //do nothing; } #endregion } else { value = (jValue as JValue).Value; if (value is string) { var propertyType = mp.PropertyType; if (propertyType == typeof(byte[])) { value = Convert.FromBase64String(value as string); } else { //兼容处理枚举的 Label 值。 var innerType = TypeHelper.IgnoreNullable(propertyType); if (innerType.IsEnum) { value = EnumViewModel.Parse(value as string, innerType); } } } } #endregion entity.SetProperty(mp, value, ManagedPropertyChangedSource.FromUIOperating); } } else { //PersistenceStatus:如果指定了状态,则主动设置该实体的状态。 if (propertyName.EqualsIgnoreCase(this.PersistenceStatusProperty)) { #region 处理:PersistenceStatus var value = (jValue as JValue).Value; var status = (PersistenceStatus)Enum.Parse(typeof(PersistenceStatus), value.ToString(), true); entity.PersistenceStatus = status; #endregion } //TreeChildren:如果指定了树子节点列表,则也需要加载进来。 else if (propertyName.EqualsIgnoreCase(this.TreeChildrenProperty)) { #region 处理:TreeChildren var jArray = jValue as JArray; if (jArray != null) { var treeChildren = entity.TreeChildren; for (int i = 0, c = jArray.Count; i < c; i++) { var child = this.DeserializeEntity(entity.GetType(), jArray[i] as JObject); treeChildren.LoadAdd(child); } treeChildren.MarkLoaded(); } #endregion } else if (this.UnknownAsDynamicProperties) { #region 处理动态属性 var jValueObj = jValue as JValue; if (jValueObj != null) { entity.SetDynamicProperty(propertyName, jValueObj.Value); } #endregion } } } //可以使用 Json.NET 来遍历给实体属性赋值。 //using (var jsonTextReader = new StringReader(strContent)) //{ // var jsonSerializer = JsonSerializer.Create(this.SerializerSettings); // jsonSerializer.Populate(jsonTextReader, entity); //} }