Skip to content

Instantly share code, notes, and snippets.

@lawrencekgrant
Created September 19, 2012 00:42
Show Gist options
  • Save lawrencekgrant/3746968 to your computer and use it in GitHub Desktop.
Save lawrencekgrant/3746968 to your computer and use it in GitHub Desktop.
List of types to CSV (without headers)
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace deflect
{
class Wizard
{
public string Name {get;set;}
public string Class {get;set;}
public string Quote {get;set;}
}
class MainClass
{
public static List<Wizard> wizards = new List<Wizard>();
static void Main(string [] args)
{
wizards = new List<Wizard>() {
new Wizard() { Name = "Bill Gates", Class = "CEO", Quote="640K ought to be enough for anybody."},
new Wizard() { Name = "Steve Wozniak", Class= "Engineer", Quote="Everything we did we were setting the tone for the world."},
new Wizard() { Name = "Warren Buffett", Class= "Accountant", Quote="takes 20 years to build a reputation and five minutes to ruin it. If you think about that, you'll do things differently."}
};
var stringBuilder = new StringBuilder();
foreach(var wiz in wizards)
{
List<string> props = new List<string>();
foreach(var prop in typeof(Wizard).GetProperties())
{
props.Add(prop.GetValue(wiz, null).ToString());
}
stringBuilder.AppendLine(string.Join(",", props.ToArray()));
}
Console.WriteLine(stringBuilder.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment