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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:38:52+00:00 2026-05-20T15:38:52+00:00

How can I get the list of elements and datatypes of an array of

  • 0

How can I get the list of elements and datatypes of an array of an object in c# with reflections?

Scenario:
I have a method with an array parameter in my web service (asmx). Using reflection I am reading the method parameters and loop through the properties. Here is my code:


Example:
I have a webservice http://localhost/services/myservice.asmx. It has a method string GetServiceData(context mycontext).
context class has following properties

- string name
- Account[] accounts

Account in turn has

- string acct_name
- string acct_address

I need to read the service dynamically to generate the following output

<GetServiceData>
    <mycontext>
        <name>string</name>
        <accounts>
            <acct_name>string</acct_name>
            <acct_address>string</acct_address>
        </accounts>
    </mycontext>
</GetServiceData>

To do so I am dynamically reading the MethodInfo and get all the parameters. Then I loop through the parameters to get the list of all the properties and the datatypes.
If there is an array element in the properties then I need to get all the elements of that array element.

Solution (Thanks to Ani)


foreach (MethodInfo method in methods)
{
    sb.Append("<" + method.Name + ">");
    ParametetInfo parameters = method.GetParameters();
    foreach(ParameterInfo parameter in parameters)
    {
        sb.Append("<" + parameter.Name + ">");
        if (IsCustomType(parameter.ParameterType))
        {
           sb.Append(GetProperties(parameter.ParameterType));
        }
        else
        {
            sb.Append(parameter.ParameterType.Name);
        }
        sb.Append("</" + parameter.Name + ">");
    }
    sb.Append("</" + sMethodName + ">");
}

GetProperties() method reads the type and actually loop through each property and add it to string object. In this method I want to check if the property is an array then get all the elements and type for it.

public static string GetProperties(Type type)
{
    StringBuilder sb = new StringBuilder();
    foreach(PropertyInfo property in type.GetProperties())
    {
        sb.Append("<" + property.Name + ">");

        if (property.PropertyType.IsArray)
        {
            Type t = property.PropertyType.GetElementType();
            sb.Append(GetProperties(t));
        }
        else
        {
            sb.Append(property.PropertyType.name);
        }
    }
    sb.ToString();
}
  • 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-20T15:38:53+00:00Added an answer on May 20, 2026 at 3:38 pm

    I think you’re for looking for theType.IsArray property (indicates whether a type is an array) and the Type.GetElementType method (gets, amongst other things, the element-type of an array). Do note that the element-type of the array isn’t necessarily the same as the specific run-time type of each of the elements of the array; polymorphism could come into play.

    Of course, to simply get the values, you can rely on array-covariance: cast the value of the property (I assume you’re using PropertyInfo.GetValue somewhere) to an object[] and foreach that as usual.

    EDIT:

    Your update is quite confusing. If you already have an object that you think might be an array; you might as well do:

    object obj = ...
    
    object[] array = obj as object[];
    
    if(array != null)
    {
       foreach(object item in array)
       {
          ...
       }      
    }
    

    It really appears like you’re confusing meta-data with real-data here. You can’t actually enumerate any array if all you have is a System.Type that represents an array-type.

    EDIT:

    I think I’ve finally understood what you want to do. Just use Type.GetElementType to get the array’s element-type, and then get that type’s properties (recursively?). You’re probably going to change your design a bit to able to get the XML output you want. XML represents a hierarchy; but your method’s return-type is just a Dictionary<string, string>, which is a flat data-structure.

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

Sidebar

Related Questions

I have one table having ID and other attributes. How can I get list
How can I get all list elements having a <span> element with class=required <form
I have a treeview, with some elements in it. How can I get a
You can get a list of databases using PRAGMA database_list or a list of
There's a function in PHP where you can get a list of the user
I am building a pretty basic form app. I can get a list of
Is there any debugging options built-in in the Xlib (libX11.so)? Can I get list
Can I get a list of the playlists and songs in each playlist in
Where can I get a list of browser differences in the implementation of DOM?
How can I to get list of all wcf services running on a machine?

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.