Reputation: 533
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());
}
}
};
}
}
Upvotes: 1
Views: 624
Reputation: 13676
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());
}
Upvotes: 1