jeudi 24 novembre 2016

generic creation of a comma separated string list from a collection

I have the following code. The first line should be the comman separated headers string and the remaining should be the content of my collection layed out to be written to a comma separated file. How do i get the values out?

public static List<string> ToListOfStrings<T>(this List<T> items, string sep)
{
    var NewList = new List<string>();
    //string tempHeaderString = "";

    PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

    string s = string.Empty;
    foreach (var prop in props)
    {
        s += prop.Name + sep;

    }
    NewList.Add(s + System.Environment.NewLine);
    foreach (var item in items)
    {
        string s1 = string.Empty;
        var values = new object[props.Length];
        for (var i = 0; i < props.Length; i++)
        {
            s1 += (props[i].GetValue(item, null)).ToString() + sep;
        }

        NewList.Add(s1 + System.Environment.NewLine);
    }

    return NewList;
}

Best

B





Aucun commentaire:

Enregistrer un commentaire