Created
September 19, 2012 00:42
-
-
Save lawrencekgrant/3746968 to your computer and use it in GitHub Desktop.
List of types to CSV (without headers)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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