1

I have a rather simple set of classes with properties only, such as:

using System;               //main data types
using System.Reflection;    //to iterate through all properties of an object
using System.Collections;   //for IEnumerable implementation?

namespace ConsoleApp1
{
    public class WholeBase //: IEnumerable ?
    {
        public SomeHeaders Headers { get; set; }
        public SomeBody Body { get; set; }
    }

    public partial class SomeHeaders
    {
        public string HeaderOne { get; set; }
        public string HeaderTwo { get; set; }
    }

    public partial class InSet
    {
        public string AllForward { get; set; }
        public string Available { get; set; }
    }

    public partial class SomeBody
    {
        public InSet MySet { get; internal set; }
        public Boolean CombinedServiceIndicator { get; set; }
        public int FrequencyPerDay { get; set; }
        public string ValidUntil { get; set; }
    }

I was trying to get all properties and values, but seems I am stuck because IEnumerable or something is missing. This is what I have tried so far: populate properties and tried to loop through all properties and values, however, doesn't work...

  public class Program
{
    //...
    public static void Main(string[] args)
    {
        WholeBase NewThing = new WholeBase();
        NewThing.Headers = new SomeHeaders { HeaderOne = "First", HeaderTwo = "Second" };

        NewThing.Body = new SomeBody
        {
            MySet = new InSet { AllForward = "YES", Available = "YES"},
            CombinedServiceIndicator = false,
            FrequencyPerDay = 10,
            ValidUntil = "2019-12-31"
        };

        void SeeThrough(WholeBase myBase)
        {
            //iterate through all the properties of NewThing
            foreach (var element in myBase)
            {
                foreach (PropertyInfo prop in myBase.GetType().GetProperties())
                {
                    var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                    Console.WriteLine(prop.GetValue(element, null).ToString());
                }
            }
        };
    }
}
5
  • 2
    Your class does not need to implement IEnumerable in order to be able to get all of its properties. What is whrong woith your current approach? Do you get any error? Commented Dec 18, 2019 at 12:37
  • You don´t need two loops. A simngle one is sufficient (the inner one, by the way) Commented Dec 18, 2019 at 12:39
  • To use IEnumerable you need more than one item. It looks like all your properties are singletons. The easy way if possible is just make the items arrays or lists. Commented Dec 18, 2019 at 12:40
  • @jdweng Thank you very much for the advice, I'm new and don't know how to implement Lists with nested objects, specially with the public class WholeBase for example.. Commented Dec 18, 2019 at 13:26
  • The you need a variable : List<WholeBase> wholeBase = new List<WholeBase>(); and then add items to the list. Commented Dec 18, 2019 at 13:53

1 Answer 1

1

Well, it seems that you're thinking "hmm I gonna loop through all values of properties of class 'A' while using reflection to get all properties of class 'A' and then for each property I'm gonna display its value."

There is a number of things wrong here.

Firstly - Looping through all values is only possible with an object that Implements interface IEnumerable but you don't really need that. Since you get all of its Properties using reflection you can use it to get values as well:

foreach (PropertyInfo prop in myBase.GetType().GetProperties())
{
    // this returns object
    var element = prop.GetValue(myBase, null);
    Console.WriteLine(element);
}

Secondly - ToString() doesn't know how to display object's fields unless object overrides it.

Although the code above will compile and work but because element is an object and not a primitive type unless you have overriden the method .ToString this call Console.WriteLine will only display the type's name.

You could once again loop through all the properties of this object and finally get a value for each property:

foreach (var childProperty in element.GetType().GetProperties())
{
   Console.WriteLine(childProperty.GetValue(element, null).ToString());
}
Sign up to request clarification or add additional context in comments.

2 Comments

@HimBromBeere Yes, I confused smth. Thank you
@Fabjan Yes, you guessed my intention, and thank you very much for the code, it is so helpful, I'm almost there. I want to combine both, sort of having: Console.WriteLine(element + "=" + childProperty.GetValue(element).ToString()); but obviously in this way element, null has to be changed, an ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.