Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8898761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:41:35+00:00 2026-06-15T00:41:35+00:00

I have a task that I am sure can be easily generalized in a

  • 0

I have a task that I am sure can be easily generalized in a simple way.

I have different tables to be exported to excel dynamically, and what I am doing is creating the table columns in HTML from the Object parameters, and the rows using a foreach on the List to create the rows.

So for example if I have a list of Customer (List<Customer> )
I create the tr basing on the amount and name of the Object properties:

public class Customer
{
    public int DeliveryAddressId { get; set; }
    public DateTime CreatedDate { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    ....
}

var output = "<table>";
        output += "<thead>";
        output += "<tr>";
        output += "<th>DeliveryAddressId </th>";
        .....

and the foreach to fill in the rows:

foreach (var customer in List<Customer>)
        {
            output += "<tr>";
            output += "<td>" + customer.DeliveryAddressId + "</td>";
            .....

But then I have different objects with different parameters, so the question is:

How can I create a generic method that takes a List<Object> as input and create this kind of table using the object parameter names as columns name and then cycles the rows accordingly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:41:36+00:00Added an answer on June 15, 2026 at 12:41 am

    If you create a generic method that accepts a list of T

    public string DumpToTable<T>(List<T> list) {
    }
    

    You could enumerate all public properties on T like so:

    var props = typeof(T).GetProperties(BindingFlags.Public);
    

    Depending on your use case, you might want to make that .GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly); to filter out inherited members.

    For each PropertyInfo element, you can then access their .Name, when generating the header.

    After that, iterating through the list, you’d have to invoke their relative .GetValue methods:

    foreach(T item in list) {
        string line = "";
        foreach(PropertyInfo prop in props) {
             line += string.Format("<td>{0}</td>", prop.GetValue(item));
        }
        output += string.Format("<tr>{0}</tr>", line);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script task that executes a dynamically created SQL restore statement. This
I have a task that runs periodically 10 second. I do some picturebox refreshing
I have a task that takes a rather long time and should run in
I have a task that's driving me crazy because i have no clue where
Just stumbled upon propertyGrid and its awesome! However, i have one task that i
I have a periodic task that needs to execute once a minute (using delayed_job).
I have an MSBuild task that executes (among other things) a call to xcopy.
I have a scheduled task that is very IO intensive (deleting hundreds of thousands
I have a Fabric task that needs to access the settings of my Django
I have a Symfony task that generates some files calls exec to a jar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.