简体   繁体   中英

how to get data from a list inside a list in a class : where T:class

i was working in a class that can export a csv with all data of every class type, the think, is that i have problems exporting a class that have a list inside a list and inside another list , and i don0t know how to get that data just with the propertyInfo, i already try using linq querys, but the same problem comes to me .

i have problems exporting the type of class C:

 public class a
 {
    private string _val {get;set;}
    public string value1{get{return _val;}set{_val = value;}}
 }

 public class b{
   private List<a> _list1 = new List<a>();
   public List<a> list_1{get{return _list1;}set{_list1=value;}}
 }
 public class c{
   private List<b> _list2 = new List<b>();
   pulic List<b> list_2  {get{return _list2;}set{_list2=value;}}
 }

this is the class that i use to try to export the csv file:

namespace MvcWufooApi.Utilerias
{
    public class CsvExport<T> where T :class
     //public class CsvExport<T,D> where T :class
  //  where D:class
{
    public List<T> Objects;
    public CsvExport(List<T> objects)
    {
        Objects = objects;
    }
    public string Export()
    {
        return Export(true);
    }
    public string Export(bool includeHeaderLine)
    {
        StringBuilder sb = new StringBuilder();
        //Get properties using reflection.
        //IList<PropertyInfo> propertyInfos = typeof(T).GetProperties(BindingFlags.Public|BindingFlags.Static);
        PropertyInfo[] propertyInfos = typeof(T).GetProperties();
        if (includeHeaderLine)
        {
            //add header line.
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                sb.Append(propertyInfo.Name).Append(",");
            }
            sb.Remove(sb.Length - 1, 1).AppendLine();
        }
        //add value for each property.
        foreach (T obj in Objects)
        {
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                sb.Append(MakeValueCsvFriendly(propertyInfo.GetValue(obj, null))).Append(",");
            }
            sb.Remove(sb.Length - 1, 1).AppendLine();
        }
        return sb.ToString();
    }
    //export to a file.
    public void ExportToFile(string path)
    {
        File.WriteAllText(path, Export());
    }
    private bool IsList(object objeto){
        return objeto is IList ||
            IsGenericList(objeto);
    }
    private bool IsGenericList(object objeto) {
        var type = objeto.GetType();
        return type.IsGenericType
            && typeof(List<>) == type.GetGenericTypeDefinition();
    }
    //export as binary data.
    public byte[] ExportToBytes()
    {
        return Encoding.UTF8.GetBytes(Export());
    }
    //get the csv value for field.
    private string MakeValueCsvFriendly(object value)
    {
        if (value == null) return "";
        if (value is Nullable && ((INullable)value).IsNull) return "";
        if (value is DateTime)
        {
            if (((DateTime)value).TimeOfDay.TotalSeconds == 0)
                return ((DateTime)value).ToString("yyyy-MM-dd");
            return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
        }
        string output = value.ToString();
        if (output.Contains(",") || output.Contains("\""))
            output = '"' + output.Replace("\"", "\"\"") + '"';
        return output;
    }
  } 
  }

and this is how i use the CsvExport Class: // list = new List(); CsvExport csv = new CsvExport(list)

"...a class that can export a csv with all data of every class type" -- looking through the code, this assertion is false. MakeValueCsvFriendly() cannot handle any complex objects or lists, as it just ultimately calls ToString() on everything.

Your CSVExport class has IsList and IsGenericList but it's not called anywhere, making me think whoever wrote it intended to support lists, but never bothered with the implementation. You're going to need to check for the existence of a list in MakeValueCSVFriendly just like it's doing with DateTime and customize the output accordingly.

Another problem is logically exporting a list of lists to a flat representation like CSV. What are you expecting it to look like? Should the items in the child list appear as comma-separated items within one of the "fields" of the parent list?

      list_2
"item1, item2, item3"
"item4, item5, item6"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM