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

The Archive Base Latest Questions

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

The function below accepts an object, which can sometimes be an array of a

  • 0

The function below accepts an object, which can sometimes be an array of a given type. In this case, I suppose the type could be determined with obj[0].GetType(), assuming the array has at least one member. I would like to convert such an array to a generic List<T> of appropriate type, but the code below only succeeds in converting to List<object>. How can this be done?

        public object GetDeserializedObject(object obj, Type targetType)
        {
            if (obj is Array)
            {
                List<object> obj2 = new List<object>();
                for (int i = 0; i < ((Array)obj).Length; i++)
                {
                    obj2.Add(((object[])obj)[i]);
                }
                obj = obj2;
            }
            return obj;
        }

Note that GetSerializedObject() implements a function belonging to the IDataContractSurrogate interface, so I don’t think I can change its signature as shown.

  • 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-13T06:18:17+00:00Added an answer on May 13, 2026 at 6:18 am

    Assuming you don’t know the type at compile-time, you’ll want to create a generic method to do it, and then call it by reflection. For example:

    private static List<T> ConvertArray<T>(Array input)
    {
        return input.Cast<T>().ToList(); // Using LINQ for simplicity
    }
    
    public static object GetDeserializedObject(object obj, Type targetType)
    {
        if (obj is Array)
        {
            MethodInfo convertMethod = typeof(...).GetMethod("ConvertArray",
                BindingFlags.NonPublic | BindingFlags.Static);
            MethodInfo generic = convertMethod.MakeGenericMethod(new[] {targetType});
            return generic.Invoke(null, new object[] { obj });
        }
        return obj;
    }
    

    (If you do know the type at compile-time, just make it a generic method and call Cast and ToList directly.)

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

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Before PHP 5.2, you can use the JSON PECL extension.… May 13, 2026 at 9:13 pm
  • Editorial Team
    Editorial Team added an answer You can use the method valid() to invoke the validator… May 13, 2026 at 9:13 pm
  • Editorial Team
    Editorial Team added an answer The "Missing index" hint that is displayed is your best… May 13, 2026 at 9:13 pm

Related Questions

I'm trying to work with the google maps API and am having some trouble.
When designing libraries, I often end up resorting to the following pattern, which I
When a C++ function accepts an std::vector argument, the usual pattern is to pass
I have a javascript function that, in most cases, needs to do something with
I know how to use JSON to create objects, but there doesn't seem to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.