public override object GetValue(object component) { var mp = this.Property; var entity = component as Entity; if (mp is IRefProperty) { if (mp is IRefIdProperty) { return(entity.GetRefNullableId(mp as IRefIdProperty)); } if (mp is IRefEntityProperty) { return(entity.GetRefEntity(mp as IRefEntityProperty)); } } if (mp is IListProperty) { return(entity.GetLazyList(mp as IListProperty)); } var value = entity.GetProperty(mp); //枚举值在为界面返回值时,应该返回 Label if (value != null && TypeHelper.IsEnumNullable(mp.PropertyType)) { value = EnumViewModel.EnumToLabel((Enum)value).Translate(); } return(value); }
/// <summary> /// Formats the enum values. /// </summary> /// <param name="enumType">Type of the enum.</param> /// <returns></returns> private string FormatEnumValues(Type enumType) { var sb = new StringBuilder(); foreach (Enum item in Enum.GetValues(enumType)) { if (sb.Length > 0) { sb.AppendLine(); } sb.Append(Convert.ToInt32(item)); sb.Append(":("); sb.Append(item.ToString()); var label = EnumViewModel.EnumToLabel(item); if (!string.IsNullOrEmpty(label)) { sb.Append(", "); sb.Append(label); } sb.Append(")"); } return(sb.ToString()); }
internal static object ToClientValue(Type serverType, object value) { if (serverType.IsEnum) { value = EnumViewModel.EnumToLabel(value as Enum); } else if (serverType == typeof(Nullable <int>)) { if (value == null) { value = 0; } } else if (serverType == typeof(Nullable <Guid>)) { if (value == null) { value = Guid.Empty; } value = value.ToString(); } else if (serverType == typeof(Guid)) { value = value.ToString(); } return(value); }
protected virtual string ReadProperty(WPFEntityPropertyViewMeta property) { var data = string.Empty; try { if (property.IsReference) { var displayPath = property.DisplayPath(); var value = ObjectHelper.GetPropertyValue(this._currentRow, displayPath); if (value != null) { data = value.ToString(); } } else { var mp = property.PropertyMeta.ManagedProperty; if (mp != null) { var value = this._currentRow.GetProperty(mp); if (value != null) { if (TypeHelper.IsEnumNullable(mp.PropertyType)) { data = EnumViewModel.EnumToLabel((Enum)value).Translate(); } else { data = value.ToString(); } } } } } catch { //尽量尝试读取值,读取出错,则忽略错误。 } return(data); }
/// <summary> /// 序列化某个指定的属性的值。 /// </summary> /// <param name="value"></param> protected virtual void SerializeValue(object value) { //if (value is byte[]) //{ // var base64 = Convert.ToBase64String(value as byte[]); // _writer.WriteValue(base64); //} //else if (value is IList && !(value is byte[])) { _writer.WriteStartArray(); var list = value as IList; for (int i = 0, c = list.Count; i < c; i++) { var item = list[i]; _writer.WriteValue(item); } _writer.WriteEndArray(); } else { if (value != null && value.GetType().IsEnum) { switch (this.EnumSerializationMode) { case EnumSerializationMode.String: value = value.ToString(); break; case EnumSerializationMode.EnumLabel: value = EnumViewModel.EnumToLabel((Enum)value) ?? value.ToString(); break; default: break; } } _writer.WriteValue(value); } }
private void SerializeProperty(IProperty property, object value) { switch (property.Category) { case PropertyCategory.List: if (this.SerializeAggt && value != null) { this.WritePropertyName(property.Name); this.SerializeList(value as EntityList); } break; case PropertyCategory.LOB: case PropertyCategory.Normal: case PropertyCategory.Readonly: case PropertyCategory.Redundancy: case PropertyCategory.ReferenceId: this.WritePropertyName(property.Name); //if (value is byte[]) //{ // var base64 = Convert.ToBase64String(value as byte[]); // _writer.WriteValue(base64); //} //else if (value is IList && !(value is byte[])) { _writer.WriteStartArray(); var list = value as IList; for (int i = 0, c = list.Count; i < c; i++) { var item = list[i]; _writer.WriteValue(item); } _writer.WriteEndArray(); } else { if (value != null && value.GetType().IsEnum) { switch (_enumSerializationMode) { case EnumSerializationMode.String: value = value.ToString(); break; case EnumSerializationMode.EnumLabel: value = EnumViewModel.EnumToLabel((Enum)value) ?? value.ToString(); break; default: break; } } _writer.WriteValue(value); } break; //ReferenceId 也都全部直接输出。 //case PropertyCategory.ReferenceId: // var refProperty = property as IRefProperty; // switch (refProperty.ReferenceType) // { // case ReferenceType.Child: // case ReferenceType.Normal: // this.WritePropertyName(property.Name); // _writer.WriteValue(value); // break; // case ReferenceType.Parent: // break; // default: // throw new NotSupportedException(); // } // break; case PropertyCategory.ReferenceEntity: if (this.SerializeReference && value != null && (property as IRefProperty).ReferenceType != ReferenceType.Parent) { this.WritePropertyName(property.Name); this.SerializeEntity(value as Entity); } break; default: throw new NotSupportedException(); } }