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); |
GetOrder does not work with latest version of FileHelpers.
In older version Order property was internal and it was access using reflection (GetOrder method), with new version Order property is now public (our GetOrder method does not cater to public properties) so it can be accessed directly.
Code Changes:
- Get rid of GetOrder method
- Update
Let order = attrs.OfType(Of FieldOrderAttribute)().Single().GetOrder()
to
Let order = attrs.OfType(Of FieldOrderAttribute)().Single().Order
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:
[DelimitedRecord(",") , IgnoreFirst(1)]
public class SystemConfig
{
[FieldOrder(1), FieldTitle("Id")]
public int? Id { get; set; }
[FieldOrder(2), FieldTitle("Key")]
public string Key { get; set; }
[FieldOrder(3), FieldTitle("ApplicationName")]
public string ApplicationName { get; set; }
[FieldOrder(4), FieldTitle("ApplicationModule")]
public string ApplicationModule { get; set; }
[FieldOrder(5), FieldTitle("ServerName")]
public string ServerName { get; set; }
[FieldOrder(6), FieldTitle("ValueString")]
public string ValueString { get; set; }
}
Edited code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace FileHelpers
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class FieldTitleAttribute : Attribute
{
public FieldTitleAttribute(string name)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
}
public string Name { get; private set; }
}
public static class FileHelpersTypeExtensions
{
public static IEnumerable<string> GetFieldTitles(this Type type)
{
var props = from prop in type.GetProperties(
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance)
where prop.IsFileHelpersProperty()
select prop;
return from prop in props
let attrs = prop.GetCustomAttributes(true)
let order = attrs.OfType<FieldOrderAttribute>().Single().Order
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 IsFileHelpersProperty(this PropertyInfo prop)
{
var tmp = prop.GetCustomAttributes(true)
.OfType<FieldOrderAttribute>()
.Any();
return tmp;
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
VB.NET version should anyone need this:-