Example #1
0
        /// <summary>Get formatted values class properties containing <see cref="DataTableAttribute"/> indicators</summary>
        /// <param name="instance">Instance</param>
        public static Collection <object> GetDataTableValues <TData>(this TData instance) where TData : class
        {
            Collection <object> values = new Collection <object>();

            foreach (PropertyInfo property in typeof(TData).GetProperties())
            {
                object[] customAttributes = property.GetCustomAttributes(true);
                foreach (object customAttribute in customAttributes)
                {
                    DataTableAttribute dataTableAttribute = customAttribute as DataTableAttribute;
                    if (dataTableAttribute != null)
                    {
                        values.Add(property.GetValue(instance));
                        break;
                    }
                }
            }
            return(values);
        }
Example #2
0
        /// <summary>Get formatted values class properties containing <see cref="DataTableAttribute"/> indicators</summary>
        /// <param name="instance">Instance</param>
        /// <param name="formatProvider">Format provider</param>
        public static Collection <string> GetDataTableFormattedValues <TData>(this TData instance, IFormatProvider formatProvider) where TData : class
        {
            Collection <string> values = new Collection <string>();

            foreach (PropertyInfo property in typeof(TData).GetProperties())
            {
                object[] customAttributes = property.GetCustomAttributes(true);
                foreach (object customAttribute in customAttributes)
                {
                    DataTableAttribute dataTableAttribute = customAttribute as DataTableAttribute;
                    if (dataTableAttribute != null)
                    {
                        var value = property.GetValue(instance);
                        if (value == null)
                        {
                            values.Add("");
                        }
                        else
                        {
                            string style = dataTableAttribute.Format;
                            if (string.IsNullOrEmpty(style))
                            {
                                values.Add(value.ToString());
                            }
                            else
                            {
                                IFormattable formattableValue = value as IFormattable;
                                if (formattableValue == null)
                                {
                                    values.Add(value.ToString());
                                }
                                else
                                {
                                    values.Add(formattableValue.ToString(style, formatProvider));
                                }
                            }
                        }
                        break;
                    }
                }
            }
            return(values);
        }
Example #3
0
        /// <summary>Get styles of class containing <see cref="DataTableAttribute"/> indicators</summary>
        /// <param name="type">Type</param>
        /// <exception cref="ArgumentNullException"/>
        public static Collection <string> GetDataTableStyles(this Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            Collection <string> styles = new Collection <string>();

            foreach (PropertyInfo property in type.GetProperties())
            {
                object[] customAttributes = property.GetCustomAttributes(true);
                foreach (object customAttribute in customAttributes)
                {
                    DataTableAttribute dataTableAttribute = customAttribute as DataTableAttribute;
                    if (dataTableAttribute != null)
                    {
                        styles.Add(dataTableAttribute.Format);
                        break;
                    }
                }
            }
            return(styles);
        }