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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:48:39+00:00 2026-05-21T13:48:39+00:00

I have a object[] containig some values. I want to extract infomation out of

  • 0

I have a object[] containig some values. I want to extract infomation out of it, but I fail at casting the second object in the array (a WeakReference) to a IList with T as the third value in the array.

Take a look at my code:

object[] vals = GetValues(); //vals[2] = WeakReference, vals[3] = Type of T, vals[4] = index in the list
IList<((Type)vals[3])> list = (IList<((Type)vals[3])>)(((WeakReference)vals[2]).Target); //This line does not even compile, seems like I'm doing something wrong..
object oo = list.ElementAt((int)vals[4]);
//Do something with oo...

Any suggestions how I can cast the Target of the WeakReference to a IList interface with T = vals[3] ?

  • 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-21T13:48:40+00:00Added an answer on May 21, 2026 at 1:48 pm

    It’s really strange that you are packing so much heterogeneous information into an array. Arrays are normally used to store elements of the same type. Why not encapsulate the data in a proper type?

    But to answer the question as asked – in C# 4, you can use dynamic:

    var target = ((dynamic)vals[2]).Target;
    
    if(target != null)
    {
        object oo = Enumerable.ElementAt(target, vals[4]);
        //Do something with oo...
    }
    

    (EDIT: If you want to minimize the use of dynamic here, cast to a WeakReference and leave the dynamic call to the end. This way, type-safety is ‘maximized’.)

    Otherwise, you can use reflection:

    object target = ((WeakReference)vals[2]).Target;
    
    if (target != null)
    {
        object oo = target.GetType()
                          .GetProperty("Item")
                          .GetValue(target, new[] { vals[4] });
        //Do something with oo...
    }
    

    (EDIT: If the indexer could be explicitly implemented, you’ll probably need to use the interface mappings.)

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

Sidebar

Related Questions

I have an existing StringBuilder object, the code appends some values and a delimiter
I have a List which contains some values. Example: List<object> testData = new List
I have a data() object containing some json. Is there a way I can
i have resultset object and it containing some country names. e.g. rs={India, Japan, USA,
I have a page where I want to display some points on map. I
I have a problem to extract a JasonObject from a Arry, that contains some
I have a method that reads some XML and populates an object. This is
I have a priority_queue of some object: typedef priority_queue<Object> Queue; Queue queue; From time
I have a simple MVC3 application that I want to retrieve some configuration details
I am not a statistics guy but have to deal with quite some data.

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.