0
            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);

Charlieface
  • 52,284
  • 6
  • 19
  • 43
  • 1
    Does this help with your problem? https://stackoverflow.com/questions/1954746/using-reflection-in-c-sharp-to-get-properties-of-a-nested-object – GH DevOps Jul 20 '22 at 16:14

1 Answers1

0

You can get the values of any class instance like this:

var myClass = new MyClass { Id = 100 };

Get the type of the class using .GetType(). On the type you can retrieve all the properties. It returns an array of PropertyInfo.

var properties = myClass.GetType().GetProperties();

Iterate over the array and then you can get the value using the .GetValue(obj?) handing it the object you used to get the type and properties from:

foreach (var prop in properties)
{
    object val = prop.GetValue(myClass);

    Console.WriteLine(val);
}

public class MyClass
{
    public int Id { get; set; }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paul Sinnema
  • 2,534
  • 2
  • 20
  • 33