ReflectionExtensions.GetProperties C# (CSharp) Method

GetProperties() public static method

public static GetProperties ( this type, BindingFlags, flags ) : IEnumerable
type this
flags BindingFlags,
return IEnumerable
    public static IEnumerable<PropertyInfo> GetProperties(this Type type, BindingFlags flags)
    {
        return type.GetTypeInfo().DeclaredProperties;
    }

Usage Example

        /// <summary>
        /// Creates data table from source data.
        /// </summary>
        public static DataTable ToDataTable <T>(this IEnumerable <T> source)
        {
            DataTable table = new DataTable();

            //// get properties of T
            var binding = BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty;
            var options = PropertyReflectionOptions.IgnoreEnumerable | PropertyReflectionOptions.IgnoreIndexer | PropertyReflectionOptions.IgnoreClasses;

            var properties = ReflectionExtensions.GetProperties <T>(binding, options).ToList();

            //// create table schema based on properties
            foreach (var property in properties)
            {
                table.Columns.Add(property.Name,
                                  property.PropertyType.Name == "Nullable`1"
                        ? property.PropertyType.GenericTypeArguments[0]
                        : (property.PropertyType.BaseType == typeof(Enum) ? typeof(int) : property.PropertyType));
            }

            //// create table data from T instances
            object[] values = new object[properties.Count];

            foreach (T item in source)
            {
                for (int i = 0; i < properties.Count; i++)
                {
                    values[i] = properties[i].GetValue(item, null);
                }

                table.Rows.Add(values);
            }

            return(table);
        }
All Usage Examples Of ReflectionExtensions::GetProperties