How can I automatically turn an IEnumerable<T> into a csv file?
<p>I am trying to create a user control that accepts any generic list so I can iterate through it and create a CSV export. Is it possible to expose a public property that can accept any type? (i.e. <code>List&lt;Product&gt;</code>, <code>List&lt;Customer&gt;</code>, etc.) If yes, how?</p>

<pre><code>public IEnumerable&lt;T&gt; AnyList { get; set; }
</code></pre>

<p>Here's what I have as far as the utility methods I have:</p>

<pre><code>public static byte[] ToCsv&lt;T&gt;(string separator, IEnumerable&lt;T&gt; objectlist)
{
    //Type t = typeof(T); Deleted this line.

    //Here's the line of code updated.
    PropertyInfo[] propertyNames = objectlist.First().GetType().GetProperties();

    string header = String.Join(separator, propertyNames.Select(f =&gt; f.Name).ToArray());

    StringBuilder csvdata = new StringBuilder();
    csvdata.AppendLine(header);

    foreach (var o in objectlist)
        csvdata.AppendLine(ToCsvFields(separator, propertyNames, o));

    return Encoding.ASCII.GetBytes(csvdata.ToString());
}

public static string ToCsvFields(string separator, PropertyInfo[] fields, object o)
{
    StringBuilder linie = new StringBuilder();

    foreach (var f in fields)
    {
        if (linie.Length &gt; 0)
            linie.Append(separator);

        var x = f.GetValue(o, null);

        if (x != null)
            linie.Append(x.ToString());
    }

    return linie.ToString();
}
</code></pre>
