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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:16:26+00:00 2026-05-13T20:16:26+00:00

I wrote this extension method: public static DataTable ToDataTable<T>(this IList<T> list) {…} It works

  • 0

I wrote this extension method:

public static DataTable ToDataTable<T>(this IList<T> list)
{...}

It works well if called with a type known at compile time:

DataTable tbl = new List<int>().ToDataTable();

But how to call it if the generic type isn’t known?

object list = new List<int>();
...
tbl = Extension.ToDataTable((List<object>)list); // won't work
  • 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-13T20:16:26+00:00Added an answer on May 13, 2026 at 8:16 pm

    This occurs because a List<int> is not a List<object> — the List type is not covariant in its element type parameter. Unfortunately you would need to get a typed version of the generic method and call it using reflection:

    Type listItemType = typeof(int);   // cheating for simplicity - see below for real approach
    MethodInfo openMethod = typeof(Extension).GetMethod("ToDataTable", ...);
    MethodInfo typedMethod = openMethod.MakeGenericMethod(typeof(listItemType));
    typedMethod.Invoke(null, new object[] { list });
    

    An alternative may be to create a version of your extension method that accepts IList rather than IList<T>. The List<T> class implements this non-generic interface as well as the generic interface, so you will be able to call:

    public static DataTable WeakToDataTable(this IList list) { ... }
    
    ((IList)list).WeakToDataTable();
    

    (In reality you’d probably use an overload rather than a different name — just using a different name to call out the different types.)


    More info: In the reflection solution, I skipped over the problem of how to determine the list element type. This can be a bit tricky depending on how sophisticated you want to get. If you’re assuming that the object will be a List<T> (for some T) then it’s easy:

    Type listItemType = list.GetType().GetGenericArguments()[0];
    

    If you’re only willing to assume IList<T> then it’s a bit harder, because you need to locate the appropriate interface and get the generic argument from that. And you can’t use GetInterface() because you’re looking for a closed constructed instance of a generic interface. So you have to grovel through all the interfaces looking for one which is an instance of IList<T>:

    foreach (Type itf in list.GetType().GetInterfaces())
    {
      if (itf.IsGenericType && itf.GetGenericTypeDefinition == typeof(IList<>))  // note generic type definition syntax
      {
        listItemType = itf.GetGenericArguments()[0];
      }
    }
    

    This will work for empty lists because it goes off the metadata, not the list content.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.