Esempio n. 1
0
        /// <summary>
        /// 替换打印的占位符
        /// </summary>
        /// <typeparam name="T">要取值的实体对象</typeparam>
        /// <param name="obj">要取值的实体对象值</param>
        /// <param name="json_dataList"></param>
        /// <returns></returns>
        public string Replace <T>(T obj, string json_dataList)
        {
            PropertyInfo[] properArray = typeof(T).GetProperties();
            Object         value       = null;
            string         str         = "";

            foreach (var proper in properArray)
            {
                if (proper.GetCustomAttribute(typeof(PrintAttribute), false) != null)
                {
                    value = proper.GetValue(obj, null);
                    if (value == null)
                    {
                        value = "";
                    }
                    if (proper.PropertyType == typeof(string))
                    {
                        str = StringHelper.NullToEmpty(value.ToString());
                    }
                    else if (proper.PropertyType == typeof(int?) || proper.PropertyType == typeof(int))
                    {
                        str = StringHelper.NullToEmpty(value == null ? "" : value.ToString());
                    }
                    else if (proper.PropertyType == typeof(DateTime) || proper.PropertyType == typeof(DateTime?))
                    {
                        str = WIPCommon.FormatDateTime((DateTime?)value);
                    }
                    //替换
                    json_dataList = json_dataList.Replace("[" + proper.Name + "]", str);
                }
            }
            return(json_dataList);
        }