Skip to content

Instantly share code, notes, and snippets.

@rdingwall
Created November 24, 2011 14:07
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save rdingwall/1391429 to your computer and use it in GitHub Desktop.
Save rdingwall/1391429 to your computer and use it in GitHub Desktop.
Set FileHelper's FileHelperEngine.HeaderText via reflection
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);
}
}
}
[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);
@NomadPhil
Copy link

VB.NET version should anyone need this:-

Imports System.Reflection
Imports FileHelpers

Namespace FileHelpers
    ' Thanks to Richard Dingwall @ https://gist.github.com/rdingwall/1391429
    Public Module FileHelpersTypeExtensions
        <Runtime.CompilerServices.Extension> _
        Public Function GetFieldTitles(type As Type) As IEnumerable(Of String)
            Dim fields = From field In type.GetFields(
                BindingFlags.GetField Or 
                BindingFlags.Public Or 
                BindingFlags.NonPublic Or 
                BindingFlags.Instance)
                    Where field.IsFileHelpersField()
                    Select field

            Return From field In fields
                Let attrs = field.GetCustomAttributes(True)
                Let order = attrs.OfType(Of FieldOrderAttribute)().Single().GetOrder()
                Let title = attrs.OfType(Of FieldTitleAttribute)().Single().Name
                Order By order
                Select title
        End Function

        <Runtime.CompilerServices.Extension> _
        Public Function GetCsvHeader(type As Type) As String
            Return String.Join(",", type.GetFieldTitles())
        End Function

        <Runtime.CompilerServices.Extension> _
        Private Function IsFileHelpersField(field As FieldInfo) As Boolean
            Return field.GetCustomAttributes(True).OfType(Of FieldOrderAttribute)().Any()
        End Function

        <Runtime.CompilerServices.Extension> _
        Private Function GetOrder(attribute As FieldOrderAttribute) As Integer
            ' Hack cos FieldOrderAttribute.Order is internal (why?)
            Dim pi = GetType(FieldOrderAttribute).GetProperty(
                "Order", 
                BindingFlags.GetProperty Or 
                BindingFlags.Instance Or 
                BindingFlags.Public Or 
                BindingFlags.NonPublic)

            Return CInt(pi.GetValue(attribute, Nothing))
        End Function
    End Module
End NameSpace

<DelimitedRecord(","), IgnoreFirst(1)>
Public Class Person
     <FieldOrder(1), FieldTitle("Name")>
     Public Name As String
     <FieldOrder(2), FieldTitle("Age")>
     Public Age As Integer
End Class

Dim importEngine = New FileHelperEngine(Of Person)() With {
     .HeaderText = GetType(Person).GetCsvHeader()
}

@rohitsies
Copy link

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:

  1. Get rid of GetOrder method
  2. Update
    Let order = attrs.OfType(Of FieldOrderAttribute)().Single().GetOrder()
    to
    Let order = attrs.OfType(Of FieldOrderAttribute)().Single().Order

@jompa234
Copy link

jompa234 commented Jan 16, 2019

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