Exemple #1
0
        public static T TryParse <T>(string value, T defaultValue)
        {
            object r = null;

            using (ErrorScope es = new ErrorScope())
            {
                if (TryParse(typeof(T), value, out r))
                {
                    return((T)r);
                }
                else
                {
                    es.IgnoreError <ErrorInfo>();
                }
            }
            return(defaultValue);
        }
Exemple #2
0
        protected virtual void DoGetFromForm()
        {
            foreach (PropertyInfo propertyInfo in GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
            {
                if (!propertyInfo.IsDefined(typeof(FilterItemAttribute), true))
                {
                    continue;
                }

                FilterItemAttribute attribute = (FilterItemAttribute)(propertyInfo.GetCustomAttributes(typeof(FilterItemAttribute), true)[0]);

                string formName = attribute.FormName;

                if (string.IsNullOrEmpty(formName))
                {
                    formName = propertyInfo.Name;
                }

                string text = HttpContext.Current.Request.Form[formName];

                if (text != null)
                {
                    //此时可以认为Filter不为空
                    if (IsNull)
                    {
                        this.IsNull = false;
                    }

                    switch (Type.GetTypeCode(propertyInfo.PropertyType))
                    {
                    case TypeCode.String:
                        text = HttpUtility.HtmlEncode(text);
                        propertyInfo.SetValue(this, text, null);
                        break;

                    default:
                        //暂时先这么写了,以后要改善这部分的设计
                        if (attribute.FormType == FilterItemFormType.BeginDate || attribute.FormType == FilterItemFormType.EndDate)
                        {
                            DateTime?date = null;
                            if (!string.IsNullOrEmpty(text))
                            {
                                using (ErrorScope es = new ErrorScope())
                                {
                                    if (attribute.FormType == FilterItemFormType.EndDate)
                                    {
                                        date = DateTimeUtil.ParseEndDateTime(text);
                                    }
                                    else if (attribute.FormType == FilterItemFormType.BeginDate)
                                    {
                                        date = DateTimeUtil.ParseBeginDateTime(text);
                                    }
                                    es.IgnoreError <ErrorInfo>();
                                }
                                propertyInfo.SetValue(this, date, null);
                            }
                            break;
                        }


                        if (propertyInfo.PropertyType == typeof(ExtendedFieldSearchInfo))
                        {
                            propertyInfo.SetValue(this, ExtendedFieldSearchInfo.Parse(text), null);
                        }
                        else
                        {
                            object objValue;
                            using (ErrorScope es = new ErrorScope())
                            {
                                if (StringUtil.TryParse(propertyInfo.PropertyType, text, out objValue))
                                {
                                    propertyInfo.SetValue(this, objValue, null);
                                }

                                es.IgnoreError <ErrorInfo>();
                            }
                        }
                        break;
                    }
                }
            }
        }
Exemple #3
0
        protected virtual void DoParse(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            try
            {
                value = SecurityUtil.DesDecode(value);
            }
            catch
            {
                return;
            }

            int index = value.IndexOf('|');

            if (index < 1)
            {
                return;
            }

            int[] valueLengths = StringUtil.Split <int>(value.Substring(0, index), ',');

            if (valueLengths.Length == 0)
            {
                return;
            }

            //此时可以认为Filter不为空
            this.IsNull = false;

            index++;

            int i = 0;

            foreach (PropertyInfo propertyInfo in GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
            {
                if (!propertyInfo.IsDefined(typeof(FilterItemAttribute), true))
                {
                    continue;
                }

                if (valueLengths.Length <= i)
                {
                    break;
                }

                int valueLength = valueLengths[i];

                FilterItemAttribute attribute = (FilterItemAttribute)(propertyInfo.GetCustomAttributes(typeof(FilterItemAttribute), true)[0]);

                string text = value.Substring(index, valueLength);
                switch (Type.GetTypeCode(propertyInfo.PropertyType))
                {
                case TypeCode.Boolean:
                    if (text == "1")
                    {
                        propertyInfo.SetValue(this, true, null);
                    }
                    else
                    {
                        propertyInfo.SetValue(this, false, null);
                    }
                    break;

                case TypeCode.String:
                    propertyInfo.SetValue(this, text, null);
                    break;

                default:
                    //暂时先这么写了,以后要改善这部分的设计

                    if (attribute.FormType == FilterItemFormType.BeginDate || attribute.FormType == FilterItemFormType.EndDate)
                    {
                        DateTime?date = null;
                        if (!string.IsNullOrEmpty(text))
                        {
                            using (ErrorScope es = new ErrorScope())
                            {
                                if (attribute.FormType == FilterItemFormType.EndDate)
                                {
                                    date = DateTimeUtil.ParseEndDateTime(text);
                                }
                                else if (attribute.FormType == FilterItemFormType.BeginDate)
                                {
                                    date = DateTimeUtil.ParseBeginDateTime(text);
                                }
                                es.IgnoreError <ErrorInfo>();
                            }
                            propertyInfo.SetValue(this, date, null);
                        }
                        break;
                    }

                    if (propertyInfo.PropertyType == typeof(ExtendedFieldSearchInfo))
                    {
                        propertyInfo.SetValue(this, ExtendedFieldSearchInfo.Parse(text), null);
                    }
                    else
                    {
                        object objValue;
                        using (ErrorScope es = new ErrorScope())
                        {
                            if (StringUtil.TryParse(propertyInfo.PropertyType, text, out objValue))
                            {
                                propertyInfo.SetValue(this, objValue, null);
                            }

                            es.IgnoreError <ErrorInfo>();
                        }
                    }
                    break;
                }

                index += valueLength;
                i++;
            }
        }