C# 反射抓屬性

  • 1467
  • 0
  • C#
  • 2012-05-16

反射,屬性

PropertyInfo[] columnProps = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo columnProp in columnProps)
            {
                object[] attributes = columnProp.GetCustomAttributes(typeof(DataGridColumnAttribute), false);
                if (attributes != null && attributes.Length > 0)
                {
                    DataGridColumnAttribute attr = attributes[0] as DataGridColumnAttribute;

                    string name = columnProp.Name;
                    string header = attr.Header;
                }