Propertyinfo 反射返回 Int32 不是我需要的实际 属性 名称

Propertyinfo reflection returning Int32 not actual property names I need

我有以下代码,我试图获取 属性 名称供以后使用。但是,每当我 运行 它输出 属性 类型作为 Int32 及其伴随名称时,我可以在 visual studio 中看到它,但是反射类型是正确的。如果它有助于修复对象,我传入的是一个数组。我在其他地方使用过这段代码,它在没有数组的情况下也能正常工作,所以这可能就是问题所在。

我传递一个对象而不是确切类型的原因是我多次重复使用该方法并且重载不是一个选项:

public void ExportResults(object fix, string fileName)
        {
            if (string.IsNullOrWhiteSpace(FileExport))
            {
                MessageBox.Show("File export cannot be empty", "Error", MessageBoxButton.OK);
            }
            else if (!string.IsNullOrWhiteSpace(FileExport))
            {
                StringBuilder sb = new StringBuilder();
                using (var wbook = new XLWorkbook())
                {
                    var rec = (object[])fix;
                    int col = 1;
                    var ws = wbook.Worksheets.Add("Sheet1");
                    foreach (var propertyInfo in from propertyInfo in fix.GetType().GetProperties()
                                                 where !propertyInfo.Name.Contains("ExtensionData")
                                                 select propertyInfo)
                    {
                        int row = 1;
                        ws.Cell(row++, col).Value = propertyInfo.Name;

                        for (int i = 0; i < rec.Length; i++)
                        {
                            ws.Cell(row++, col).Value = propertyInfo.GetValue(rec[i]);
                        }

                        col++;
                    }

                    for (int i = 0; i < rec.Length; i++)
                    {
                        foreach (var propertyInfo in from propertyInfo in fix.GetType().GetProperties()
                                                     where !propertyInfo.Name.Contains("ExtensionData")
                                                     select propertyInfo)
                        {
                            sb.AppendLine($"{propertyInfo.Name}: {propertyInfo.GetValue(rec[i])}");
                        }

                        sb.AppendLine();
                    }
           }

编辑:

所以我已经通过以下方式解决了我的问题。但是我有很多评论说这对性能来说不是很好,我应该缓存属性这是如何实现的?

public void ExportResults(object fix, string fileName)
        {
            if (string.IsNullOrWhiteSpace(FileExport))
            {
                MessageBox.Show("File export cannot be empty", "Error", MessageBoxButton.OK);
            }
            else if (!string.IsNullOrWhiteSpace(FileExport))
            {
                StringBuilder sb = new StringBuilder();
                using (var wbook = new XLWorkbook())
                {
                    var rec = (object[])fix;
                    int col = 1;
                    var prop = 0;
                    var ws = wbook.Worksheets.Add("Sheet1");

                    foreach (var propertyInfo in from propertyInfo in rec[prop].GetType().GetProperties()
                                                 where !propertyInfo.Name.Contains("ExtensionData")
                                                 select propertyInfo)
                    {
                        int row = 1;
                        ws.Cell(row++, col).Value = propertyInfo.Name;

                        for (int i = 0; i < rec.Length; i++)
                        {
                            ws.Cell(row++, col).Value = propertyInfo.GetValue(rec[i]);
                        }

                        col++;
                        prop++;
                    }
                    

                    for (int i = 0; i < rec.Length; i++)
                    {
                        foreach (var propertyInfo in from propertyInfo in rec[i].GetType().GetProperties()
                                                     where !propertyInfo.Name.Contains("ExtensionData")
                                                     select propertyInfo)
                        {
                            sb.AppendLine($"{propertyInfo.Name}: {propertyInfo.GetValue(rec[i])}");
                        }

                        sb.AppendLine();
                    }
          }

ClosedXML 已经允许您使用 InsertTable or InsertData. InsertTable returns a table that allows formatting, displaying filters and totals etc

从任何 IEnumerable<T> 将数据插入 sheet
  var wb = new XLWorkbook();
  var ws = wb.Worksheets.Add("Test Sheet");

  var list = new List<Person>();
  list.Add(new Person() { Name = "John", Age = 30, House = "On Elm St."   });
  list.Add(new Person() { Name = "Mary", Age = 15, House = "On Main St."  });
  list.Add(new Person() { Name = "Luis", Age = 21, House = "On 23rd St."  });
  list.Add(new Person() { Name = "Henry", Age = 45, House = "On 5th Ave." });

  var tableWithPeople = ws.Cell(1, 1).InsertTable(list);
  tableWithPeople .Theme = XLTableTheme.TableStyleLight10;

  ws.Columns().AdjustToContents();
  wb.SaveAs("InsertingTables.xlsx");

将项目列表保存到 sheet 的方法可以短至:

public void SaveExcel<T>(IEnumerable<T> items,string path,string sheet="Sheet1")
{
  var wb = new XLWorkbook();
  var ws = wb.Worksheets.Add(sheet);

  var table= ws.Cell(1, 1).InsertTable(items);
  table.Theme = XLTableTheme.TableStyleLight10;

  ws.Columns().AdjustToContents();
  wb.SaveAs(path);
}