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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:51:00+00:00 2026-06-11T11:51:00+00:00

I have a method that reads some XML and populates an object. This is

  • 0

I have a method that reads some XML and populates an object. This is done via reflection based on the element names in the XML mathing the property names on the object. This works for basic object types but i am struggling with enum arrays.

So I have a PropertyInfo object (lets call it property) for my enum array property and I have a string value (lets call it value) containing comma separated numbers representing the enum values (e.g. “1,3,5”).

I have tried:

property.SetValue(this, value.Split(',').Select(i => int.Parse(i)).ToArray(), null);

and

property.SetValue(this, value.Split(',').Select(i => Enum.ToObject(property.PropertyType.GetElementType(), int.Parse(i))).ToArray(), null);

But no joy. In the first code example the result of the Select.ToArray is an int[] which then throws a parse error. Similar case with the second but the Select.ToArray returns an object[] and again throws a parse error.

I want to write this as if the enum type is unknown.

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-06-11T11:51:01+00:00Added an answer on June 11, 2026 at 11:51 am

    try this:

    // create the enum values as object[]
    var values = value.Split(',')
      .Select(i => Enum.Parse(property.PropertyType.GetElementType(), i))
      .ToArray()
    
    // create array of correct type
    Array array = (Array)Activator.CreateInstance(property.PropertyType, values.Length);
    
    // copy the object values to the array
    int i = 0;
    foreach(var value in values)
    {
      array.SetValue(value, i++);
    }
    
    // set the property
    property.SetValue(this, array, null)
    

    There might be a simpler way, but the key is that you create an array of the correct type.

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

Sidebar

Related Questions

I have a class, say, CDownloader, that reads some XML data and provides access
I have a project that reads some data from xml file Convert.ToDouble metod works
I have code that reads an XML file. Some of the attributes of elements
I have a static method that validates some XML. In order to validate the
I have a method that reads data from a comma separated text file and
I have a webservice method that reads a photo and returns its byte data.
I have method that returns Drawable , and if its Bitmap object is recycled
I have made a class XmlHandler that extends DefaultHandler and reads XML. The XML
I've been given some XML files that don't quite have a proper schema (I
I have developed a website in asp.net mvc that reads from a xml-file to

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.