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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:49:02+00:00 2026-05-24T18:49:02+00:00

So lets say I have a class like public class Complex { } //

  • 0

So lets say I have a class like

public class Complex { } // this is what I mean when I say List<Complex>
// its just a user-defined class or type if you will

Now let’s say I have a method which returns a list that holds objects.

Now let’s say I can further guarantee that each of these objects in the list is actually of type Complex (in other words List < Complex >)

I would like the most painless way of casting this list as possible. A one-liner would be ideal, but a few lines would be fine too.

Here is what I tried (but doesn’t work – InvalidCastOperation exception):

// Sorry this is so hard to read! Also .Cast<Complex>() doesn't work either :(

return (ComplexCollection) ((List<Complex>) ((List<object>) complexElementsDictionary["ComplexElementCollection"]).OfType<Complex>()); 

Some specifics about this code snippet:

ComplexCollection inherits List<Complex>
complexElementsDictionary is of type (Dictionary<string, List<object>)

So in plain terms I’m asking a Dictionary<> for its corresponding list of objects. Then I try to cast this list to what its supposed to be, which is a list of Complex.

Note – Please try to ignore the fact that I am casting like this at runtime. I’m deserializing an XML collection (in app.config actually) and this is how I’ve chosen to do it. There may be a better way, but for now I just want to see if anyone knows a way to do what I’m asking.

Thank you so much! I hope it is clear what I’m asking, and if not I’ll update the question.

  • 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-24T18:49:02+00:00Added an answer on May 24, 2026 at 6:49 pm

    To convert a List<object> to List<Complex> is as simple as:

    List<object> objectList = complexElementsDictionary["ComplexElementCollection"];
    List<Complex> complexList = objectList.Cast<Complex>().ToList();
    

    However you can’t cast a List<Complex> to ComplexCollection just because ComplexCollection inherits from List<Complex>. (the other way around is fine though)

    List<T> has a constructor which takes an IEnumerable<T> and adds the elements to a list. So I think what you need is to add this constructor to ComplexCollection:

    public ComplexCollection(IEnumerable<Complex> values) : base(values)
    {
        /* plus any other logic you have */
    }
    

    And then your example with the return statement becomes:

    return new ComplexCollection(
        complexElementsDictionary["ComplexElementCollection"].Cast<Complex>()
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a class like this in class1.vb: Public Class my_class Public
Lets say I have a class like this in Java: public class Function {
Lets say i have a class like this class book { public $title; public
Lets say I have a two classes like this: public class LocalResources { public
Lets say I have a class like this: public class Foo { public Foo
Lets say I have a controller class like this. public class InStorePickupController : Controller
Lets say I have a set of model classes like this: public class Person
Lets say I have a custom data type that looks something like this: public
lets say I have a class like this: public class MyClass { private String
Let's say I have data structures that're something like this: Public Class AttendenceRecord Public

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.