Skip to content

Instantly share code, notes, and snippets.

@Warpten
Created October 12, 2014 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Warpten/c792ad66ad20cc69a918 to your computer and use it in GitHub Desktop.
Save Warpten/c792ad66ad20cc69a918 to your computer and use it in GitHub Desktop.
ObjectListView Generator, add support for fields
diff --git a/ObjectListView/Utilities/Generator.cs b/ObjectListView/Utilities/Generator.cs
index 95454ca..57506fb 100644
--- a/ObjectListView/Utilities/Generator.cs
+++ b/ObjectListView/Utilities/Generator.cs
@@ -199,18 +199,40 @@ public class Generator : IGenerator {
if (type == null)
return columns;
- // Iterate all public properties in the class and build columns from those that have
- // an OLVColumn attribute and that are not ignored.
- foreach (PropertyInfo pinfo in type.GetProperties()) {
- if (Attribute.GetCustomAttribute(pinfo, typeof(OLVIgnoreAttribute)) != null)
- continue;
-
- OLVColumnAttribute attr = Attribute.GetCustomAttribute(pinfo, typeof(OLVColumnAttribute)) as OLVColumnAttribute;
- if (attr == null) {
- if (allProperties)
- columns.Add(this.MakeColumnFromPropertyInfo(pinfo));
- } else {
- columns.Add(this.MakeColumnFromAttribute(pinfo, attr));
+ if (allProperties)
+ {
+ // Iterate all public properties in the class and build columns from those that have
+ // an OLVColumn attribute and that are not ignored.
+ foreach (PropertyInfo pinfo in type.GetProperties()) {
+ if (Attribute.GetCustomAttribute(pinfo, typeof(OLVIgnoreAttribute)) != null)
+ continue;
+
+ OLVColumnAttribute attr = Attribute.GetCustomAttribute(pinfo, typeof(OLVColumnAttribute)) as OLVColumnAttribute;
+ if (attr == null) {
+ if (allProperties)
+ columns.Add(this.MakeColumnFromPropertyInfo(pinfo));
+ } else {
+ columns.Add(this.MakeColumnFromAttribute(pinfo, attr));
+ }
+ }
+ }
+ else
+ {
+ // Iterate all public fields in the class and build columns from those that have
+ // an OLVColumn attribute and that are not ignored.
+ const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
+
+ foreach (FieldInfo pinfo in type.GetFields(flags)) {
+ if (Attribute.GetCustomAttribute(pinfo, typeof(OLVIgnoreAttribute)) != null)
+ continue;
+
+ OLVColumnAttribute attr = Attribute.GetCustomAttribute(pinfo, typeof(OLVColumnAttribute)) as OLVColumnAttribute;
+ if (attr == null) {
+ if (!allProperties)
+ columns.Add(this.MakeColumnFromFieldInfo(pinfo));
+ } else {
+ columns.Add(this.MakeColumnFromAttribute(pinfo, attr));
+ }
}
}
@@ -269,6 +291,16 @@ public class Generator : IGenerator {
}
/// <summary>
+ /// Create a column from the given FieldInfo and OLVColumn attribute
+ /// </summary>
+ /// <param name="pinfo"></param>
+ /// <param name="attr"></param>
+ /// <returns></returns>
+ protected virtual OLVColumn MakeColumnFromAttribute(FieldInfo pinfo, OLVColumnAttribute attr) {
+ return MakeColumn(pinfo.Name, DisplayNameToColumnTitle(pinfo.Name), false, pinfo.GetType(), attr);
+ }
+
+ /// <summary>
/// Create a column from the given PropertyInfo and OLVColumn attribute
/// </summary>
/// <param name="pinfo"></param>
@@ -286,6 +318,15 @@ public class Generator : IGenerator {
protected virtual OLVColumn MakeColumnFromPropertyInfo(PropertyInfo pinfo) {
return MakeColumn(pinfo.Name, DisplayNameToColumnTitle(pinfo.Name), pinfo.CanWrite, pinfo.PropertyType, null);
}
+
+ /// <summary>
+ /// Make a column from the given FieldInfo
+ /// </summary>
+ /// <param name="pinfo"></param>
+ /// <returns></returns>
+ protected virtual OLVColumn MakeColumnFromFieldInfo(FieldInfo pinfo) {
+ return MakeColumn(pinfo.Name, DisplayNameToColumnTitle(pinfo.Name), false, pinfo.GetType(), null);
+ }
/// <summary>
/// Make a column from the given PropertyDescriptor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment