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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:06:38+00:00 2026-06-12T19:06:38+00:00

I have an IList<object> where every single object is an instance of a type

  • 0

I have an IList<object> where every single object is an instance of a type T (which I don’t know at compile-time).

I need an IList<T> out of this. I can’t use Cast since I don’t know the type at compile time, and there isn’t a Cast(Type) overload I could use.

This is what I currently have in place:

private object Map(IList<ApiCallContext> bulk)
{
    // god-awful way to build a IEnumerable<modelType> list out of Enumerable<object> where object is modelType.
    // quoting lead: "think whatever you do it will be ugly"
    Type modelType = model.Method.ModelType;

    if (bulk.Count > 0)
    {
        modelType = bulk.First().Parameters.GetType();
    }
    Type listType = typeof(List<>).MakeGenericType(modelType);
    object list = Activator.CreateInstance(listType);
    foreach (object value in bulk.Select(r => r.Parameters))
    {
        ((IList)list).Add(value);
    }
    return list;
}

What I’m thinking about is maybe I could create a new LooseList class that implements IList and just works around the casting, seems better than what I currently have but it still sounds way too clunky.

  • 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-12T19:06:39+00:00Added an answer on June 12, 2026 at 7:06 pm

    If you really need to do exactly as you’ve stated, I’d first separate this out into “context-specific code” and “reusable code”. Effectively you want something like this:

    public static IList ToStrongList(this IEnumerable source, Type targetType)
    

    I would implement that by writin a strongly-typed method, and then calling it via reflection:

    private static readonly MethodInfo ToStrongListMethod = typeof(...)
        .GetMethod("ToStrongListImpl", BindingFlags.Static | BindingFlags.NonPublic);
    
    public static IList ToStrongList(this IEnumerable source, Type targetType)
    {
        var method = ToStrongListMethod.MakeGenericMethod(targetType);
        return (IList) method.Invoke(null, new object[] { source });
    }
    
    private static List<T> ToStrongListImpl<T>(this IEnumerable source)
    {
        return source.Cast<T>().ToList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic dictonary which is templated in the following manner: Dictionary<object, IList<ISomeInterface>>
I Have a Nhibernate object called Car, this Car object have a PersietentBag IList
Say i have a object like this public class Student{ public IList<Coursework> Courseworks{get;set;} public
I have a object OtherFaclilityEntity which contains a IList of OtherFcs objects, and the
I have a object structure like this: public class Entity { IList<Relationship> Relationships{get;set;} }
Suppose I have public IList<Entity> Children { get; set; } public NotifyChildren(Func<object, bool> action,
the ToSelectList method I have: public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty,
I have a class CustomCollection : IList which maintains a list of CustomObject Types.
I have a list of type IList<Effort> . The model Effort contains a float
I have a List<> of custom objects. This custom type has a property called

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.