简体   繁体   中英

cannot load Json data via reflection

I have a data stored in Json. I can dynamically create the datatable columns but when i load the data, its not loaded.

  dt = new DataTable();
             Type firstType =new  TickerData().GetType();
           foreach (PropertyInfo propertyInfo in firstType.GetProperties())
             {
                 dt.Columns.Add(propertyInfo.Name, propertyInfo.PropertyType);
             }

            foreach (string file in filePaths)
            {

                TickerData td=getSymbolJson(file);


                DataRow newrow =dt.NewRow();
                foreach (PropertyInfo propertyInfo in td.GetType().GetProperties())
                {
                    newrow[propertyInfo.Name] = propertyInfo.GetValue(td, null);
                }
                dt.ImportRow(newrow);

            }

I am missing something. Can you spot it? The data is there in the json. i can track it in visual studio

newrow has the data as you can see from the watch window 在此处输入图片说明

But the dataset visualizer shows no data for the datatable. 在此处输入图片说明

use dt.Rows.Add(newrow); instead of dt.ImportRow(newrow);

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