Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have a look at <a href="http://www.codeproject.com/KB/linq/LINQtoCSV.aspx" rel="noreferrer">LINQ to CSV</a>. Although it's a little on the heavy side, which is why I wrote the following code to perform just the small subset of functionality that I needed. It handles both properties and fields, like you asked for, although not much else. One thing it does do is properly escape the output in case it contains commas, quotes, or newline characters.</p> <pre><code>public static class CsvSerializer { /// &lt;summary&gt; /// Serialize objects to Comma Separated Value (CSV) format [1]. /// /// Rather than try to serialize arbitrarily complex types with this /// function, it is better, given type A, to specify a new type, A'. /// Have the constructor of A' accept an object of type A, then assign /// the relevant values to appropriately named fields or properties on /// the A' object. /// /// [1] http://tools.ietf.org/html/rfc4180 /// &lt;/summary&gt; public static void Serialize&lt;T&gt;(TextWriter output, IEnumerable&lt;T&gt; objects) { var fields = from mi in typeof (T).GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) where new [] { MemberTypes.Field, MemberTypes.Property }.Contains(mi.MemberType) let orderAttr = (ColumnOrderAttribute) Attribute.GetCustomAttribute(mi, typeof (ColumnOrderAttribute)) orderby orderAttr == null ? int.MaxValue : orderAttr.Order, mi.Name select mi; output.WriteLine(QuoteRecord(fields.Select(f =&gt; f.Name))); foreach (var record in objects) { output.WriteLine(QuoteRecord(FormatObject(fields, record))); } } static IEnumerable&lt;string&gt; FormatObject&lt;T&gt;(IEnumerable&lt;MemberInfo&gt; fields, T record) { foreach (var field in fields) { if (field is FieldInfo) { var fi = (FieldInfo) field; yield return Convert.ToString(fi.GetValue(record)); } else if (field is PropertyInfo) { var pi = (PropertyInfo) field; yield return Convert.ToString(pi.GetValue(record, null)); } else { throw new Exception("Unhandled case."); } } } const string CsvSeparator = ","; static string QuoteRecord(IEnumerable&lt;string&gt; record) { return String.Join(CsvSeparator, record.Select(field =&gt; QuoteField(field)).ToArray()); } static string QuoteField(string field) { if (String.IsNullOrEmpty(field)) { return "\"\""; } else if (field.Contains(CsvSeparator) || field.Contains("\"") || field.Contains("\r") || field.Contains("\n")) { return String.Format("\"{0}\"", field.Replace("\"", "\"\"")); } else { return field; } } [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] public class ColumnOrderAttribute : Attribute { public int Order { get; private set; } public ColumnOrderAttribute(int order) { Order = order; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload