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 6995363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:00:46+00:00 2026-05-27T20:00:46+00:00

How to create a list dynamically from a class name (which is passed as

  • 0

How to create a list dynamically from a class name (which is passed as an argument) and return the list in C#.

Below code may not work. I have posted it to give an idea:

 public T ConvertDataSetToList<T>(DataSet _ds, String tableName, string className)
    {
        Type classType=Type.GetType(className);
        List<T> newList = new List<T>();

        //System.Activator.CreateInstance(Type.GetType(className));

        try
        {
            Details _Details;
            for (int iRowCount = 0; iRowCount < _ds.Tables[tableName].Rows.Count; iRowCount++)
            {
                _Details = FillDTO(_ds.Tables[tableName].Rows[iRowCount]);
                newList.Add(_msDetails);
            }
        }
        catch (Exception ex) { }
        return newList;
    }
  • 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-05-27T20:00:47+00:00Added an answer on May 27, 2026 at 8:00 pm

    You can dot it with some basic mapping stuff.

    //Maps a dataset
    public List<T> MapDataSet<T>(DataSet anyDataset
                               , string tablename) where T : new()
    {
      return MapDataTable<T>(anyDataset.Tables[tablename]);
    }
    
    // Maps a datatable
    public List<T> MapDataTable<T>(DataTable table) where T : new()
    {
      List<T> result = new List<T>();
      foreach(DataRow row in table.Rows)
      {
         result.Add(MapDataRow<T>(row));
      }
      return result;
    }
    
    // maps a DataRow to an arbitrary class (rudimentary)
    public T MapDataRow<T>(DataRow row) where T: new()
    {
      // we map columns to class properties
      Type destinationType = typeof(T);
      // create our new class
      T mappedTo = new T();
      // iterate over the columns
      for(int columnIndex=0;columnIndex<row.ItemArray.Length;columnIndex++)
      {
        // get a matching property of our class
        PropertyInfo fieldTo = destinationType.GetProperty(
                                  row.Table.Columns[columnIndex].ColumnName );
        if (fieldTo !=null)
        {
           // map our fieldvalue to our property
           fieldTo.SetValue(mappedTo, row[columnIndex], new object[] {});
        }
        else
        {
         // sorry, field doens't match any property on class
        }
      }
      return mappedTo;
    }
    

    Here is basic test app to demonstrate its usage

    void Main()
    {
        DataTable dt = new DataTable("hello");
        dt.Columns.Add("foo");
        dt.Columns.Add("bar");
        dt.Columns.Add("foobar");
    
        DataRow row = dt.NewRow();
        row[0]="blah1";
        row[1] ="two";
         row[2] = "fb1";
    
        dt.Rows.Add(row);
    
        row = dt.NewRow();
        row[0]="apples";
        row[1] ="pears";
        row[2] = "duh";
    
        dt.Rows.Add(row);
    
        List<DTO> list = MapDataTable<DTO>(dt);
        List<SecondDTO> list2 = MapDataTable<SecondDTO>(dt);
    
    }
    
    // sample DTO object
    public class DTO
    {
      public string foo { get; set;}
      public string bar { get; set; }
    }
    
    // another sample DTO object
    public class SecondDTO
    {
      public string foo { get; set;}
      public string foobar { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have list of class names and want to create their instances dynamically. for
How to dynamically create a class from its content? In fact I've create a
I create a linked list dynamically and initialize the first node in main(), and
Just wondering if it is possible to dynamically create a list of strings in
Is there a way to dynamically create a selectItem list? I dont really want
Is it possible to create a new List<T> where the T is dynamically set
if i create a html ul. and wanted to dynamically add list items to
i have a unordered list of links, which are dynamically created by Ajax, and
I want to create a list of integers from 1 to n . I
I want to dynamically add items to a class, but someone may have to

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.