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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:15:51+00:00 2026-05-16T06:15:51+00:00

LinQ contains the method Cast which casts each entry in the list to type

  • 0

LinQ contains the method Cast which casts each entry in the list to type T. Lets say we have a list which looks like the following:

List<Object> obj = new List<Object>();
obj.Add("A");
obj.Add("B");

A working cast could be

var list = obj.Cast<string>();

What I would like to work

Type t = typeof(String);
Object list = obj.Cast(t);

A solution would be to use reflection and genericly create a list and populate it but I was wondering if there exist any better solution? Heard that .NET 4.0 should support some co/contra-variance which may be a way to do it.


Extra Information and Reflection Soulution

The error I get is the following The model item passed into the dictionary is of type System.Collections.Generic.List1[IStatisticEntry], but this dictionary requires a model item of type System.Collections.Generic.List1[CrashStatistic+CrashEntry].
Note that CrashEntry implements IStaticEntry but cannot be casted because it is a generic type of the list.

I constructed the following solution through I would stille like something without Reflection:

    public static object Cast(this IEnumerable list, Type type)
    {
        var newList = Activator.CreateInstance(typeof(List<>).MakeGenericType(type));

        foreach (var item in list)
            newList.GetType().GetMethod("Add").Invoke(newList, new object[] { item });

        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-16T06:15:52+00:00Added an answer on May 16, 2026 at 6:15 am

    It’s not possible to do this statically, any more than you can do it with a single object.

    Type t = typeof(string);
    var x = (t)obj; // invalid
    

    However, it is possible to treat the elements dynamically so that a cast to the underlying type isn’t necessary:

    static void Main(string[] args)
    {
        List<Object> obj = new List<Object>();
        obj.Add("A");
        obj.Add("B");
    
        var list = obj.Cast<dynamic>();
        foreach (var item in list)
        {
            Console.WriteLine(item[0]);
        }
    
        Console.ReadKey();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found a bug in the Contains statement in Linq (not sure if it
I'm using T4 for generating repositories for LINQ to Entities entities. The repository contains
LINQ simplifies database programming no doubt, but does it have a downside? Inline SQL
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.
Using LINQ to Entities sounds like a great way to query against a database
LINQ to SQL allows table mappings to automatically convert back and forth to Enums
In LINQ to SQL, is it possible to check to see if an entity
In Linq to SQL it is possible to generate the database from the dbml
This Linq to SQL query ... Return (From t In Db.Concessions Where t.Country =
Can LINQ to SQL query using NOT IN ? e.g., SELECT au_lname, state FROM

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.