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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:34:25+00:00 2026-05-22T15:34:25+00:00

Struggling with the following problem. I have an attribute that defines the name of

  • 0

Struggling with the following problem. I have an attribute that defines the name of the key in the database table. Using reflection, I initialize the value of a property or a field with that attribute. Everything is great until I define my property as an array:

[ConfigurationKey("TestArray")]
public int[] Array { get; set; }

Assuming that values stored in the table are comma-delimited strings, I am using the following to create an array:

return valueString.Split(',').Select(s => Convert.ChangeType(s, memberType.GetElementType())).ToArray();

This does create an array of elements but only array of Objects. As a result when I use FieldInfo or PropertyInfo to set a value, it throws with the exception “Cannot assign Object[] to Int32[]“.

Any ideas?

  • 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-22T15:34:26+00:00Added an answer on May 22, 2026 at 3:34 pm

    Well given that Convert.ChangeType is declared to return Object, I don’t think this is particularly surprising. ToArray() will create an array with the same element type as the input sequence, and Select is going to return an IEnumerable<object> in this case.

    One option is to call Cast (and then ToArray) using reflection. To be honest, it’s probably going to be easiest to put everything into a single generic method, and call that by reflection:

    public static T[] ParseToArray<T>(string valueString)
    {
        return valueString.Split(',')
                          .Select(s => (T) Convert.ChangeType(s, typeof(T))
                          .ToArray();
    }
    

    Then you’d need:

    Type t = memberType.GetElementType();
    // Use the type declaring ParseToArray here
    MethodInfo methodDefinition = typeof(...).GetMethod("ParseToArray");
    MethodInfo method = methodDefinition.MakeGenericMethod(t);
    object array = method.Invoke(null, new object[] { valueString });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling with the following problem: I have a value object containing
I have searched/Googled around but I'm struggling with the following problem. I am building
I am currently struggling with the following problem. I have successfully installed Apache2 and
I'm struggling to understand why the following problem appears. I have an input box
Hi I'm struggling with the following problem. I have two tables one with employee
I have been struggling with the following problem for a long time. I have
I'm struggling with the following situation: I have a users table with authors of
I have a problem with javascript cookies that I have been struggling with for
I've been struggling with the following problem. I have a series of function objects,
I've been struggling with the following problem for some days now. I have found

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.