Beispiel #1
0
        /// <summary>
        /// 对象转文本
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="format"></param>
        /// <param name="seq"></param>
        /// <returns></returns>
        public static String ToText(this IText obj, String format = FMT_PV, String ignoreName = "", String seq = ",")
        {
            if (format == null || format == "")
            {
                format = FMT_PV;
            }
            if (seq == null || seq == "")
            {
                seq = ",";
            }

            List <String> ignoreNames = new List <String>(ignoreName.Split(','));



            MemberInfo[] memberInfos = obj.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance);

            List <String> results = new List <string>();

            for (int i = 0; i < memberInfos.Length; i++)
            {
                TransinetAttribute transinet = memberInfos[i].GetCustomAttribute <TransinetAttribute>();
                if (transinet == null)
                {
                    continue;
                }
                TextAttribute txtAttr = memberInfos[i].GetCustomAttribute <TextAttribute>();
                if (txtAttr != null && txtAttr.Ignored)
                {
                    continue;
                }
                String name = memberInfos[i].Name;
                if (ignoreNames.Contains(name))
                {
                    continue;
                }
                if (txtAttr != null && StringUtils.NotEmpty(txtAttr.ShowText))
                {
                    name = txtAttr.ShowText;
                }
                if (ignoreNames.Contains(name))
                {
                    continue;
                }

                Object value    = memberInfos[i].FindValue(obj);
                String valueStr = value == null ? "" : ConvertUtils.ConvertTo <String>(value, txtAttr != null ? txtAttr.Format : "");
                String text     = format;
                text = text.Replace("{$P}", name);
                text = text.Replace("{$V}", valueStr);
                results.Add(text);
            }
            return(results.Aggregate <String>((x, y) => x + seq + y));
        }
Beispiel #2
0
        /// <summary>
        /// 对象转字符串
        /// </summary>
        /// <param name="x"></param>
        /// <param name="format"></param>
        /// <param name="props"></param>
        /// <returns></returns>
        public static String objectToStr(this Object x, String format = "", Properties props = null)
        {
            if (x == null)
            {
                return("");
            }
            String sep = props == null ? TEXT_PROP_SEP_DEFAULT : props.Get <String>(TEXT_PROP_SEP, TEXT_PROP_SEP_DEFAULT);

            if (format == null || format == "")
            {
                format = props == null ? "" : props.Get <String>(TEXT_PROP_FORMAT, "");
            }
            String[] propertyNames = props == null ? null : props.Get <String[]>(TEXT_PROP_PROPNAMES, null);

            //基础数据类型转换
            if (x.GetType() == typeof(char))
            {
                return(x.ToString());
            }
            else if (x.GetType() == typeof(int) || x.GetType() == typeof(Int32))
            {
                return(intToStr((int)x, format, props));
            }
            else if (x.GetType() == typeof(float) || x.GetType() == typeof(double))
            {
                return(doubleToStr((double)x, format, props));
            }
            else if (x.GetType() == typeof(String))
            {
                return((String)x);
            }
            else if (x.GetType() == typeof(DateTime))
            {
                return(datetimeToStr((DateTime)x, format, props));
            }
            else if (x.GetType() == typeof(String))
            {
                return(x.ToString());
            }
            else if (x.GetType().IsEnum)
            {
                return(x.ToString());
            }
            else if (x.GetType().IsArray)
            {
                StringBuilder st = new StringBuilder();
                foreach (Object ele in (Array)x)
                {
                    if (st.ToString() != null)
                    {
                        st.Append(",");
                    }
                    st.Append(objectToStr(ele, format, props));
                }
                return(st.ToString());
            }
            else if (typeof(IEnumerable).IsInstanceOfType(x))
            {
                StringBuilder st  = new StringBuilder();
                IEnumerable   tor = (IEnumerable)x;
                foreach (Object ele in tor)
                {
                    if (st.ToString() != "")
                    {
                        st.Append(",");
                    }
                    st.Append(objectToStr(ele, format, props));
                }
                return(st.ToString());
            }



            MemberInfo[] memberInfos = x.GetType().GetProperties();

            List <String> results = new List <string>();

            for (int i = 0; i < memberInfos.Length; i++)
            {
                TransinetAttribute transinet = memberInfos[i].GetCustomAttribute <TransinetAttribute>();
                if (transinet != null)
                {
                    continue;
                }
                TextAttribute txtAttr = memberInfos[i].GetCustomAttribute <TextAttribute>();
                if (txtAttr != null && txtAttr.Ignored)
                {
                    continue;
                }
                String name = memberInfos[i].Name;
                if (propertyNames != null && propertyNames.Length > 0 && !propertyNames.Contain(name, true))
                {
                    continue;
                }


                if (txtAttr != null && StringUtils.NotEmpty(txtAttr.ShowText))
                {
                    name = txtAttr.ShowText;
                }
                Object value    = memberInfos[i].FindValue(x);
                String valueStr = value == null ? "" : ConvertUtils.ConvertTo <String>(value, txtAttr != null ? txtAttr.Format : "");
                String text     = format;
                text = text.Replace("{$P}", name);
                text = text.Replace("{$V}", valueStr);
                results.Add(text);
            }
            return(results.Aggregate <String>((a, b) => a + sep + b));
        }