Created
November 24, 2011 14:07
-
-
Save rdingwall/1391429 to your computer and use it in GitHub Desktop.
Set FileHelper's FileHelperEngine.HeaderText via reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
// see http://stackoverflow.com/questions/3975741/column-headers-in-csv-using-filehelpers-library/8258420#8258420 | |
// ReSharper disable CheckNamespace | |
namespace FileHelpers | |
// ReSharper restore CheckNamespace | |
{ | |
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] | |
public class FieldTitleAttribute : Attribute | |
{ | |
public FieldTitleAttribute(string name) | |
{ | |
if (name == null) throw new ArgumentNullException("name"); | |
Name = name; | |
} | |
public string Name { get; private set; } | |
} | |
public static class FileHelpersTypeExtensions | |
{ | |
public static IEnumerable<string> GetFieldTitles(this Type type) | |
{ | |
var fields = from field in type.GetFields( | |
BindingFlags.GetField | | |
BindingFlags.Public | | |
BindingFlags.NonPublic | | |
BindingFlags.Instance) | |
where field.IsFileHelpersField() | |
select field; | |
return from field in fields | |
let attrs = field.GetCustomAttributes(true) | |
let order = attrs.OfType<FieldOrderAttribute>().Single().GetOrder() | |
let title = attrs.OfType<FieldTitleAttribute>().Single().Name | |
orderby order | |
select title; | |
} | |
public static string GetCsvHeader(this Type type) | |
{ | |
return String.Join(",", type.GetFieldTitles()); | |
} | |
static bool IsFileHelpersField(this FieldInfo field) | |
{ | |
return field.GetCustomAttributes(true) | |
.OfType<FieldOrderAttribute>() | |
.Any(); | |
} | |
static int GetOrder(this FieldOrderAttribute attribute) | |
{ | |
// Hack cos FieldOrderAttribute.Order is internal (why?) | |
var pi = typeof(FieldOrderAttribute) | |
.GetProperty("Order", | |
BindingFlags.GetProperty | | |
BindingFlags.Instance | | |
BindingFlags.NonPublic); | |
return (int)pi.GetValue(attribute, null); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DelimitedRecord(","), IgnoreFirst(1)] | |
public class Person | |
{ | |
// Must specify FieldOrder too | |
[FieldOrder(1), FieldTitle("Name")] | |
string name; | |
[FieldOrder(2), FieldTitle("Age")] | |
int age; | |
} | |
... | |
var engine = new FileHelperEngine<Person> | |
{ | |
HeaderText = typeof(Person).GetCsvHeader() | |
}; | |
... | |
engine.WriteFile(@"C:\people.csv", people); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just stumbled upon this and did some changes to make it work with my use case.
This should work with both fields and properties.
Don't know if this is fixed in the original repo by now.
Input:
Edited code: