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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:07:05+00:00 2026-06-08T12:07:05+00:00

Goal I have an object[]Source,object Target, and FieldInfo Var (Var.FieldType.IsArray is true). I want

  • 0

Goal

I have an object[]Source,object Target, and FieldInfo Var (Var.FieldType.IsArray is true).
I want to run Var.SetValue(Target,Source).
However, I cannot convert “object[]” -> “anotherType[]”

Sample Run

object[]Source=new object[2]{"Hello","World"};
Var.SetValue(Target,Source); //Cannot Convert "object[]"->"string[]"

[note: I want to be able to use ints, doubles, floats, etc. Otherwise, this problem would be trivially easy]

Research

Use Var:
Cannot use because cannot create var arrays

Use Generics:

This works for

myField.SetValue(target,GenericCastArray<string>(source));

However, it does not work for

Type someType=typeof(string); //or int, or float
 (myfield.SetValue(target,GenericCastArray< someType > (source))

*
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/fe14d396-bc35-4f98-851d-ce3c8663cd79/* returns null reference exception, I think at this.GetType()

EDIT: Meh Solution
Based on the comments, it is impossible to convert from object[] to string[].
However, the following code works adequately well.

object[]Source=null;
Type basetype=Var.FieldType.GetElementType();
int l=somelength;
if (basetype.IsEquivalentTo(typeof(string))) Source = new string[l];
//repeat for all types

source=//run your Reflection here

var.SetValue(target,source); 
  • 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-08T12:07:06+00:00Added an answer on June 8, 2026 at 12:07 pm

    It is not possible to cast Array of one type to Array of another type (i.e. object[] to string[]) because these are different non compatible types. Reflection will not help to cast either.

    To convert you will always need to create some sort of copy of the array either directly or by creating IEnumerable<DestinationType> via one of LINQ conversion functions.

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

Sidebar

Related Questions

I have the following database table object: public class Goal { @DatabaseField(generatedId = true)
Let me first describe my goal: I have created an object with 3 properties:
goal: I have the string 1234432144 I want to only replace the first 2
My goal is to have all object files built in a .objs directory instead
I currently have a project that is a 'Business Object' project, and our goal
Final goal: Have a few java objects sharing the same base class persisted into
The goal is to have the member variable _AddValue point to the CreateFirstValue function
My goal is to have an Android phone (Samsung Galaxy Nexus) communicate with a
My goal is to have the url routing as following: http://www.abc.com/this-is-peter-page http://www.abc.com/this-is-john-page What is
My goal is to have a jQuery datepicker pop up when the user clicks

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.