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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:40:35+00:00 2026-05-16T20:40:35+00:00

I am writing some validation code. The code will take data passed into a

  • 0

I am writing some validation code. The code will take data passed into a web service and decide whether it can do the action, or return a message to the caller that they have missed out some fields etc.

I have it mostly working except for arrays. I am marking up the properties with a [RequiredField] attribute to represent fields that are required. So if this is some of my data,

public enum EnumTest
{
    Value1,
    Value2
}

[DataContract]
public class DummyWebserviceData
{
    [DataMember]
    [RequiredField]
    public EnumTest[] EnumTest{ get; set; }

    [DataMember]
    [RequiredField]
    public DummyWebserviceData2[] ArrayOfData { get; set; }
}

[DataContract]
public class DummyWebserviceData2
{
    [DataMember]
    [RequiredField]
    public string FirstName { get; set;}

    [DataMember]
    [RequiredField]
    public string LastName { get; set;}

    [DataMember]
    public string Description { get; set;}
}

So what do I have working? I have validation of dates, and strings working. It uses recursion to go any level deep required on the data.

But … so what about the two arrays there. The first is an array of enums. I want to check in that case that the array is not empty.

The second is the array of DummyWebserviceData2 values. I need to pull each value out and have a look at it individually.

To simplify the code I have written it looks something like this,

foreach (PropertyInfo propertyInfo in data.GetType().GetProperties())
{
    if (propertyInfo.PropertyType.IsArray)
    {
        // this craps out

        object[] array = (object[])propertyInfo.GetValue(data, new object[] { 0 });

    }
}

So it seems to me that the first thing is that I can tell it is an array. But how then can I tell how many items are in the array?

  • 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-16T20:40:36+00:00Added an answer on May 16, 2026 at 8:40 pm

    At runtime the object will have been dynamically subclassed from the Array data type (this MSDN topic details that), therefore you don’t need to reflect into the array, you can cast the object to Array, and then use the Array.GetValue instance method:

    Array a = (Array)propertyInfo.GetValue(data);
    for(int i = 0; i< a.Length; i++)
    {
      object o = a.GetValue(i);
    }
    

    You can also iterate over an array as well – since from .Net 2.0 onwards:

    In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic::IList, System.Collections.Generic::ICollection, and System.Collections.Generic::IEnumerable generic interfaces.

    You don’t need to know the T, since from these you can get an IEnumerable; which you can then use a Cast() operation on, or indeed just work at the object level.

    Incidentally, the reason why your code isn’t working is because you can’t cast an array of MyType[] to object[] because object[] is not a base type of MyType[] – only object is.

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

Sidebar

Related Questions

I am in the process of writing some validation code based on these assumptions:
I am writing some PHP code where I end it with a email validation
I have a list of bools on every page. I'm writing some validation code
While writing some C code, I decided to compile it to assembly and read
I am re-writing some code for a new re-design and I am wondering if
I'm writing some html code to make an html email. I've done some research
We've got a mature body of code that loads data from files into a
I am writing a Macro in excel VBA that creates a data validation list
I have been writing some code that detects add and removal of USB devices,
I'm writing some form validation functions, and I've decided to go with jQueryUI for

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.