Esempio n. 1
0
        /// <summary>
        /// returns string of headers line-printable properties
        /// </summary>
        /// <returns></returns>
        /// <exception cref="InvalidCastException"><paramref /> cannot be cast to the element type of the current <see cref="T:System.Array" />.</exception>
        public static string PrintHeadersLine(Type t)
        {
            var props      = FilterPrintable(t);
            int propsCount = props.Count;
            //	headers line
            StringBuilder header = new StringBuilder();

            object[] headers         = new object[propsCount];
            bool     fillHeadersline = true;

            //	build headers line
            foreach (var x in ForeachExt.WithIndex(props))
            {
                if (!fillHeadersline)
                {
                    continue;
                }
                if (x.Index < propsCount && fillHeadersline)
                {
                    // append header placeholder "{0, -4}"
                    header.Append("{").Append(x.Index).Append(",-4} ");
                    // set property name
                    headers.SetValue(x.Value.Name, x.Index);
                    if (x.IsLast)
                    {
                        fillHeadersline = false;
                    }
                }
            }
            return(string.Format(header.ToString(), headers) + Environment.NewLine);
        }
Esempio n. 2
0
        /// <exception><paramref /> cannot be cast to the element type of the current <see cref="T:System.Array" />.</exception>
        /// <exception cref="InvalidCastException"><paramref /> cannot be cast to the element type of the current <see cref="T:System.Array" />.</exception>
        public string ToStringAsTable()
        {
            Type t     = GetType();
            var  props = FilterPrintable(t);

            props.RemoveAll(p => p.PropertyType.IsClass);

            int propsCount = props.Count;

            StringBuilder format = new StringBuilder();

            object[] values = new object[propsCount];

            foreach (var x in ForeachExt.WithIndex(props))
            {
                PropertyInfo prop      = x.Value;
                var          val       = prop.GetValue(this);
                var          headerLen = prop.Name.Length;

                // append value placeholder "{index"
                format.Append("{").Append(x.Index);

                if (val != null)
                {
                    var enumattr = prop.GetCustomAttribute(typeof(EnumNameAttribute)) as EnumNameAttribute;
                    // display enum name
                    val = (enumattr != null) ? enumattr.GetName((int)val) : val;

                    var formatattr = prop.GetCustomAttribute(typeof(DecimalFormatAttribute)) as DecimalFormatAttribute;
                    //Console.WriteLine(prop.PropertyType.Name + "=" + prop.PropertyType.FullName);

                    string propType = prop.PropertyType.Name;

                    if (propType.Equals("Nullable`1"))
                    {
                        if (prop.PropertyType.FullName.Contains("System.Decimal"))
                        {
                            propType = "Decimal";
                        }
                        if (prop.PropertyType.FullName.Contains("System.DateTime"))
                        {
                            propType = "DateTime";
                        }
                    }
                    switch (propType)
                    {
                    case "Decimal":
                        if (formatattr != null)
                        {
                            format.Append(",-")
                            .Append(headerLen)
                            .Append(":")
                            .Append(formatattr.format)
                            .Append("} ");                                     // percent or other decimal formatting
                        }
                        else
                        {
                            format.Append(",-")
                            .Append(headerLen)
                            .Append(":F} ");                                     // other decimals
                        }
                        break;

                    case "DateTime":
                        format.Append(",-")
                        .Append(headerLen)
                        .Append(":d} ");                                 //date
                        break;

                    default:
                        format.Append(",-")
                        .Append(headerLen)
                        .Append("} ");                                 // int/string
                        break;
                    }
                }
                else
                {
                    val = novalue;
                    format.Append(",-")
                    .Append(headerLen)
                    .Append("} ");                                     // null value
                }
                values.SetValue(val, x.Index);
            }
            if (!values.Any())
            {
                return("No " + t.Name + " found.");
            }

            //Console.WriteLine(format);
            //values.ToList().ForEach(i => Console.WriteLine(","+i.ToString()));

            return(string.Format(format.ToString(), values) + Environment.NewLine);
        }