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

  • Home
  • SEARCH
  • 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 8326477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:46:29+00:00 2026-06-09T00:46:29+00:00

What is the correct code to cast an object back to a generic list

  • 0

What is the correct code to cast an object back to a generic list like so?

Type x = typeof(MyClass);
object o = new List<MyClass>();

List<x> l = o as List<x>; // Not working

EDIT:
Maybe it wasn’t all clear: The object is a list of a generic type which i don’t know at compile time.. nevertheless List has functions like “Add” i can call anyway, like:

l.Add((new MyClass() as object)) as x);
  • 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-09T00:46:31+00:00Added an answer on June 9, 2026 at 12:46 am
    List<MyClass> l = (List<MyClass>)o;
    

    or

    List<MyClass> l = o as List<MyClass>;
    

    Do you mean

    List<> l = o as List<>;
    

    This is not possible until you use a generic class:

    class C<T>
    {
        public List<T> List = new List<T>();
    }
    

    Usage:

    C<MyClass> c = new C<MyClass>();
    c.List.Add(new MyClass());
    

    Finally I got OP’s goal:

    Type listType = typeof(List<>);
    Type targetType = listType.MakeGenericType(typeof(YourClass));
    List<YourClass> list = (List<YourClass>)Activator.CreateInstance(targetType);
    

    See MSDN for details.

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

Sidebar

Related Questions

Is this code correct? I'm trying to submit it and also I would like
I have the following code (correct for my simple tests) for a linked list
this code is correct?? String note = text.txt; FileWriter file = new FileWriter(note); Scanner
EDITED SO THE CODE IS CORRECT (THANKS TO ta.speot.is) - NEW QUESTION AT BOTTOM
I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar
my code : public List<Book> GetBook(string Field, object Value) { using (EntityConnection conn =
I am trying to figure out the correct cast type for the Parent property
What is the correct code to create a link with heading 1 according to
Each time I add in the correct code, it gives me the same error
Is this code correct? std::function<int(int)> f = [&f](int n) -> int { return n

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.