Пример #1
0
        public static object GetExt(this Base_Article article, Base_CatalogExt catExt)
        {
            if (catExt == null)
            {
                return(null);
            }
            var artExt = article.Exts.FirstOrDefault(ext => ext.CatlogExtId == catExt.Id);

            if (artExt == null)
            {
                return(null);
            }

            switch ((ExtDataType)catExt.DataType)
            {
            case ExtDataType.Currency:
                return(CommOp.ToDecimalNull(artExt.Value));

            case ExtDataType.DateAndTime:
            case ExtDataType.Date:
                return(CommOp.ToDateTimeNull(artExt.Value));

            case ExtDataType.SingleNumber:
                return(CommOp.ToIntNull(artExt.Value));

            case ExtDataType.Bool:
                return(CommOp.ToBool(artExt.Value));

            default:
                return(artExt.Value);
            }
        }
Пример #2
0
        /// <summary>
        /// 获取文章的某个标签值,返回根据该文章的标签属性定义的数据类型
        /// </summary>
        /// <param name="article"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public object GetExt(Base_CatalogArticle article, string name)
        {
            Base_CatalogExt catExt = null;

            if (article != null)
            {
                catExt = SiteManager.Catalog.GetAllExts(article.CatalogId)
                         .FirstOrDefault(ext => ext.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
            }

            if (catExt == null)
            {
                return(null);
            }
            var artExt = article.Article.Exts.FirstOrDefault(ext => ext.CatlogExtId == catExt.Id);

            if (artExt == null)
            {
                return(null);
            }

            switch ((ExtDataType)catExt.DataType)
            {
            case ExtDataType.Currency:
                return(CommOp.ToDecimalNull(artExt.Value));

            case ExtDataType.DateAndTime:
            case ExtDataType.Date:
                return(CommOp.ToDateTimeNull(artExt.Value));

            case ExtDataType.SingleNumber:
                return(CommOp.ToLongNull(artExt.Value));

            case ExtDataType.Bool:
                return(CommOp.ToBool(artExt.Value));

            default:
                return(artExt.Value);
            }
        }
Пример #3
0
        /// <summary>
        /// 设置文章的某个标签值
        /// </summary>
        /// <param name="article"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public void SetExt(Base_CatalogArticle article, string name, object value)
        {
            var catExt = SiteManager.Catalog.GetAllExts(article.CatalogId)
                         .FirstOrDefault(ext => ext.Name.Equals(name, StringComparison.OrdinalIgnoreCase));

            if (catExt == null)
            {
                return;
            }
            var artExt = article.Article.Exts.First(ext => ext.CatlogExtId == catExt.Id);

            if (artExt == null)
            {
                return;
            }

            switch ((ExtDataType)catExt.DataType)
            {
            case ExtDataType.Currency:
                value = CommOp.ToDecimalNull(value);
                break;

            case ExtDataType.DateAndTime:
            case ExtDataType.Date:
                value = CommOp.ToDateTimeNull(artExt.Value);
                break;

            case ExtDataType.SingleNumber:
                value = CommOp.ToLongNull(value);
                break;

            case ExtDataType.Bool:
                value = CommOp.ToBool(value);
                break;
            }
            artExt.Value = value.ToString();
        }
Пример #4
0
        public static object SetExt(this Base_Article article, Base_CatalogExt catExt, object value)
        {
            if (catExt == null)
            {
                return(null);
            }
            var artExt = article.Exts.FirstOrDefault(ext => ext.CatlogExtId == catExt.Id);

            if (artExt == null)
            {
                artExt = new Base_ArticleExt
                {
                    CatlogExtId = catExt.Id,
                };
                article.Exts.Add(artExt);
            }
            ;

            string strValue = null;

            switch ((ExtDataType)catExt.DataType)
            {
            case ExtDataType.Currency:
                Decimal?dec = CommOp.ToDecimalNull(value);
                if (dec != null)
                {
                    strValue = dec.Value.ToString();
                }
                break;

            case ExtDataType.FloatNumber:
                double?flt = CommOp.ToDoubleNull(value);
                if (flt != null)
                {
                    strValue = flt.Value.ToString();
                }
                break;

            case ExtDataType.DateAndTime:
            case ExtDataType.Date:
                DateTime?dt = CommOp.ToDateTimeNull(value);
                if (dt != null)
                {
                    strValue = dt.Value.ToString("yyyy-MM-dd HH:mm:ss");
                }
                break;

            case ExtDataType.SingleNumber:
                long?lng = CommOp.ToLongNull(value);
                if (lng != null)
                {
                    strValue = lng.Value.ToString();
                }
                break;

            case ExtDataType.Bool:
                value    = CommOp.ToBool(value);
                strValue = value.ToString();
                break;

            default:
                strValue = CommOp.ToStr(value);
                if (strValue.IsEmpty())
                {
                    strValue = null;
                }
                break;
            }
            artExt.Value = strValue;
            return(value);
        }
Пример #5
0
        /// <summary>
        /// 将value中的值赋给对象的属性或字段
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="propName">属性名</param>
        /// <param name="value">值</param>
        /// <param name="actualType">值的类型,为空时自动判断</param>
        public static void SetValue(object obj, string propName, string value, Type actualType = null)
        {
            //if (String.IsNullOrEmpty(value)) return;
            FieldInfo fi = obj.GetType().GetField(propName, bindAttr);

            if (fi != null)
            {
                SetFieldValue(obj, fi, value, actualType);
                return;
            }
            PropertyInfo pi = GetPropertyInfo(obj, propName);

            if (pi == null)
            {
                return;
            }
            if (!pi.CanWrite)
            {
                return;
            }
            if (PraserDict.ContainsKey(pi.PropertyType))
            {
                var pvalue = PraserDict[pi.PropertyType].Prase(value);
                if (pvalue == null)
                {
                    return;
                }
                pi.SetValue(obj, pvalue, null);
                return;
            }
            if (pi.PropertyType == typeof(string) || pi.PropertyType == typeof(object))
            {
                pi.SetValue(obj, value, null);
            }
            else if (pi.PropertyType == typeof(int))
            {
                pi.SetValue(obj, CommOp.ToInt(value), null);
            }
            else if (pi.PropertyType == typeof(Int64))
            {
                pi.SetValue(obj, CommOp.ToLong(value), null);
            }
            else if (pi.PropertyType == typeof(Int16))
            {
                pi.SetValue(obj, (short)CommOp.ToInt(value), null);
            }
            else if (pi.PropertyType == typeof(int?))
            {
                pi.SetValue(obj, CommOp.ToIntNull(value), null);
            }
            else if (pi.PropertyType == typeof(bool))
            {
                pi.SetValue(obj, CommOp.ToBool(value), null);
            }
            else if (pi.PropertyType == typeof(bool?))
            {
                pi.SetValue(obj, CommOp.ToBoolNull(value), null);
            }
            else if (pi.PropertyType == typeof(char))
            {
                pi.SetValue(obj, CommOp.ToChar(value), null);
            }
            else if (pi.PropertyType == typeof(double) || pi.PropertyType == typeof(float))
            {
                pi.SetValue(obj, CommOp.ToDouble(value), null);
            }
            else if (pi.PropertyType == typeof(decimal))
            {
                pi.SetValue(obj, CommOp.ToDecimal(value), null);
            }
            else if (pi.PropertyType.IsEnum)
            {
                if (CommOp.IsNumeric(value))
                {
                    var val = CommOp.ToInt(value);
                    if (pi.PropertyType.IsEnumDefined(val))
                    {
                        pi.SetValue(obj, Enum.Parse(pi.PropertyType, val.ToString()), null);
                    }
                }
                else
                {
                    var val = CommOp.ToStr(value);
                    if (pi.PropertyType.IsEnumDefined(val))
                    {
                        pi.SetValue(obj, Enum.Parse(pi.PropertyType, val), null);
                    }
                }
            }
            else if (pi.PropertyType == typeof(DateTime))
            {
                DateTime dt = CommOp.ToDateTime(value);
                if (dt == default(DateTime))
                {
                    return;
                }
                pi.SetValue(obj, dt, null);
            }
            else if (pi.PropertyType == typeof(DateTime?))
            {
                DateTime?dt = CommOp.ToDateTimeNull(value);
                pi.SetValue(obj, dt, null);
            }
            else
            {
                actualType = actualType ?? pi.PropertyType;
                var p = JsonHelper.FormJson(value, actualType) ?? Activator.CreateInstance(actualType);
                try
                {
                    pi.SetValue(obj, p, null);
                }
                catch
                {
                    throw;
                }
            }
        }