Exemplo n.º 1
0
        internal static object ToServerValue(Type serverType, object value)
        {
            if (serverType.IsEnum)
            {
                value = EnumViewModel.LabelToEnum(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);
        }
Exemplo n.º 2
0
        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.LabelToEnum((value as string).TranslateReverse(), enumType);
                }
            }

            //使用 PropertyDescriptor 来进行操作属性时,都是作为 UI 层操作。
            entity.SetProperty(mp, value, ManagedPropertyChangedSource.FromUIOperating);
        }
Exemplo n.º 3
0
        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.LabelToEnum(value, innerType);
            }
            else
            {
                columnValue = TypeHelper.CoerceValue(_column.Property.PropertyType, value);
            }

            #endregion

            return(_f.Constraint(_column, op, columnValue));
        }
Exemplo n.º 4
0
        /// <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);
                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
                    {
                        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.LabelToEnum(value.ToString(), innerType);
                                    }
                                }
                            }
                        }

                        entity.SetProperty(mp, value, ManagedPropertyChangedSource.FromUIOperating);
                    }
                }
                else
                {
                    #region 处理:PersistenceStatus、TreeChildren

                    //PersistenceStatus:如果指定了状态,则主动设置该实体的状态。
                    if (propertyName.EqualsIgnoreCase(PersistenceStatusProperty))
                    {
                        var value  = (jValue as JValue).Value;
                        var status = (PersistenceStatus)Enum.Parse(typeof(PersistenceStatus), value.ToString(), true);
                        entity.PersistenceStatus = status;
                    }
                    //TreeChildren:如果指定了树子节点列表,则也需要加载进来。
                    else if (propertyName.EqualsIgnoreCase(AggtSerializer.TreeChildrenProperty))
                    {
                        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
                }
            }

            //可以使用 Json.NET 来遍历给实体属性赋值。
            //using (var jsonTextReader = new StringReader(strContent))
            //{
            //    var jsonSerializer = JsonSerializer.Create(this.SerializerSettings);
            //    jsonSerializer.Populate(jsonTextReader, entity);
            //}
        }