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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:13:01+00:00 2026-06-04T13:13:01+00:00

I have a little problem for you guys. I would like to do this:

  • 0

I have a little problem for you guys.

I would like to do this:

Type[] classes = new Type[]{ Class1, Class2 };

foreach(Type t in classes){   
  List<t> list = new List<t>(); 
}

Is there some way to do this?

  • 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-04T13:13:03+00:00Added an answer on June 4, 2026 at 1:13 pm

    You cannot cast to a generic type at runtime, because the type in the generic needs to be resolved at compile time.

    You can create a generic type in a dynamic manner using reflection, but unless you hard-code the cast, all you get in return is an object.

    I cannot really tell what it is you want, I have to agree with a comment this is an XY problem. I would be inclined to make the presumptuous statement that there is a design issue somewhere that this is trying to solve, instead of addressing the design issue directly, or asking the question of what you are trying to achieve directly.


    You can use the following code to create the type, then the dynamic type can be used to duck type the various members of List<T> without knowing/caring that it is a list or what T is:

    using System;
    using System.Collections.Generic;
    
    namespace ConsoleApplication61
    {
        class Program
        {
            static void Main(string[] args)
            {
                dynamic o = CreateGeneric(typeof(List<>), typeof(int));
                o.Add(1);
                Console.WriteLine(o[0]);
                Console.Read();
            }
    
            public static object CreateGeneric(Type generic, Type innerType, params object[] args)
            {
                System.Type specificType = generic.MakeGenericType(new System.Type[] { innerType });
                return Activator.CreateInstance(specificType, args);
            }
        }
    }
    

    The above sample duck types the Add method and the Indexer. The DLR does the type handling and the duck typing at runtime – knowing that 1 is an int, for example.

    Just to clarify, I likely wouldn’t use such code in production (unless you requirements are very specific to need this) and any issues with type-mismatching will occur at run time; so you either need to type very accurately (limited IntelliSense) or have good error handling.

    Thanks to this blog post for the CreateGeneric method.

    This assumes .NET 4 with the new CLR. As @MartinLiversage has also pointed out, this particular sample assumes that you are utilising the list in a sort-of-strongly-typed manner. In my example I am passing an int to a List<int> hidden in a dynamic.


    We have been on .NET 4 almost since it was released. We have a large application with an even larger code base. dynamic isn’t used once in the application, and only a few times in the test code base. That isn’t to say “don’t use it”, it’s to say “most of the time, you don’t need it”.

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

Sidebar

Related Questions

I have a little problem where I would like to insert a svn diff
Following problem: I would like to build a little clipboard with jQuery. I have
Hey guys! I have this little problem: I have one ViewController which adds 2
I have this little problem, that I cannot figure out which arguments to pass
Guys i have a little problem when i submit my form the page gets
Guys i have a little problem, i tried to find some examples on GeoIP
I have a little problem; I have this protocol defined as so below: @protocol
Hey guys, I have a little problem with a simple ajax request. I can't
Hey most of my issue has been solved but i have little problem This
I have one little problem...Here is my code..Is there a way to distribute weights

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.