mercredi 20 juillet 2022

how to get value of properties with reflection in List?

            List<T> Data = new List<T>();
            var type = typeof(T).GetProperties();

            for (int i = 0; i < Data.Count; i++)
            {
                foreach (var item in type)
                {
                    if (GetTypeOfProp(item) == 1)
                    {
                        worksheet.Range[GetCol(Col) + Row].Text = item.Name;
                        worksheet.Range[GetCol(Col) + (Row + 1)].Text = (item.GetValue(Data[i], null) ?? "").ToString();
                        Col = Col + 1;
                    }
                    else if (GetTypeOfProp(item) == 2)
                    {
                        int OldCol = Col;
                        foreach (var li in item.PropertyType.GetProperties())
                        {
                            worksheet.Range[GetCol(Col) + (Row + 1)].Text = li.Name;

                            var value = li.GetValue(item, null);     // How Can I Get this Value ?

                            Col = Col + 1;
                            MaxRecOfRow = 1;
                        }

                        worksheet.Range[$"{GetCol(OldCol) + Row}:{GetCol(Col - 1) + Row}"].Merge();
                        worksheet.Range[$"{GetCol(OldCol) + Row}:{GetCol(Col - 1) + Row}"].Text = item.Name;

                    }
                    else if (GetTypeOfProp(item) == 3)
                    {
                        worksheet.Range[GetCol(Col) + Row].Text = item.Name;
                        worksheet.Range[GetCol(Col) + (Row + 1)].Text = "";
                        Col = Col + 1;
                        MaxRecOfRow = 1;
                    }
                }
                Row++;
             }

I can get value with reflection in c# with root but this question is get value with (model in model) How Can I Get this Value ? var value = li.GetValue(item, null);





Aucun commentaire:

Enregistrer un commentaire