Example #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));
        }
Example #2
0
        public static List <String> getAlias(this INamed named)
        {
            if (named == null)
            {
                return(null);
            }
            if (named.GetType().FindMember("alias") != null)
            {
                return(new List <string>(named.FindMemberValue <List <String> >("alias")));
            }
            TextAttribute txtAttr = named.GetType().GetCustomAttribute <TextAttribute>();

            if (txtAttr == null || txtAttr.Alias == null)
            {
                return(null);
            }
            return(new List <string>(txtAttr.Alias));
        }
Example #3
0
        public static String getCaption(this INamed named)
        {
            if (named == null)
            {
                return("");
            }
            if (named.GetType().FindMember("caption") != null)
            {
                return(named.FindMemberValue <String>("caption"));
            }
            TextAttribute txtAttr = named.GetType().GetCustomAttribute <TextAttribute>();

            if (txtAttr == null)
            {
                return("");
            }
            return(txtAttr.Caption);
        }