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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:45:21+00:00 2026-05-27T05:45:21+00:00

I have the following code where I’m trying to get all the properties of

  • 0

I have the following code where I’m trying to get all the properties of an object as well as the property values. Some properties can be collections or collections of collections, so I tried to set up a recursive function for those types. Unfortunately it’s not working, erroring on this line

if (property.GetValue(item, null) is IEnumerable)

and I don’t know what needs changed. Can anyone help here? Thanks.

public static string PackageError(IEnumerable<object> obj)
{
    var sb = new StringBuilder();

    foreach (object o in obj)
    {
        sb.Append("<strong>Object Data - " + o.GetType().Name + "</strong>");
        sb.Append("<p>");

        PropertyInfo[] properties = o.GetType().GetProperties();
        foreach (PropertyInfo pi in properties)
        {
            if (pi.GetValue(o, null) is IEnumerable && !(pi.GetValue(o, null) is string))
                sb.Append(GetCollectionPropertyValues((IEnumerable)pi.GetValue(o, null)));
            else
                sb.Append(pi.Name + ": " + pi.GetValue(o, null) + "<br />");
        }

        sb.Append("</p>");
    }

    return sb.ToString();
}

public static string GetCollectionPropertyValues(IEnumerable collectionProperty)
{
    var sb = new StringBuilder();

    foreach (object item in collectionProperty)
    {
        PropertyInfo[] properties = item.GetType().GetProperties();
        foreach (var property in properties)
        {
            if (property.GetValue(item, null) is IEnumerable)
                sb.Append(GetCollectionPropertyValues((IEnumerable)property.GetValue(item, null)));
            else
                sb.Append(property.Name + ": " + property.GetValue(item, null) + "<br />");
        }
    }

    return 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-27T05:45:22+00:00Added an answer on May 27, 2026 at 5:45 am

    I would suggest using an existing serialization mechanism, such as XML serialization or JSON serialization, to provide this information if you are trying to make it generic.

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

Sidebar

Related Questions

We have following code: try { // some code throwing MyException } catch (MyException
I have following code: public abstract class Operand<T> { public T Value { get;
I have following code: private void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e) { System.Diagnostics.Process execute =
Have following code: public interface ITest { string St1 { get; } } public
I have following code in a Spring Dao which works just fine - Object
I have following code to add values in drop down, values are getting added
I have following code snippet and i am trying to evaluate the time take
I have following code to show or hide some labels depending if a radiobutton
I have following code for opening all files: int ret= open(zFile, flags, mode); posix_fadvise
I have following code for initializing bean values depending upon url parameter. <f:metadata> <f:viewParam

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.