C# — Serializing list of objects to csv

Serializing list of objects to csv with minimal code using reflection.

We will start by defining a generic method where T could be any class.

public void GenerateReport<T>(List<T> rows) where T : class
{
}

Now parse the properties of T, simply define what properties you want to include in the where linq clause. In this example I’m only selecting properties with PropertyType string, bool, char, byte, decimal, int, DateTime or DateTime?.

Keep the story going. Sign up for an extra free read.

You've completed your member preview for this month, but when you sign up for a free Medium account, you get one more story.
Already have an account? Sign in

Kristoffer Karlsson

Written by

Software developer and indie game developer from Sweden. Been turning coffee into code since 2010.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade