/// <summary>Serialize to writer.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="value"> The value.</param> /// <param name="writer">The writer.</param> public static void SerializeToWriter <T>(T value, TextWriter writer) { if (value == null) { return; } if (typeof(T) == typeof(string)) { writer.Write(value); return; } CsvSerializer <T> .WriteObject(writer, value); }
/// <summary>Serialize to string.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="value">The value.</param> /// <returns>A string.</returns> public static string SerializeToString <T>(T value) { if (value == null) { return(null); } if (typeof(T) == typeof(string)) { return(value as string); } var sb = new StringBuilder(); using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture)) { CsvSerializer <T> .WriteObject(writer, value); } return(sb.ToString()); }
/// <summary>A T extension method that converts an obj to a CSV.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="obj">The obj to act on.</param> /// <returns>obj as a string.</returns> public static string ToCsv <T>(this T obj) { return(CsvSerializer.SerializeToString(obj)); }