I have to an upper class with nested classes
public class Preferences
{
public FunctionClass function { get; set; } = new FunctionClass();
public class FunctionClass
{
public string programfolder { get; set; } = "";
...
}
public LoggerClass logger { get; set; } = new LoggerClass();
public class LoggerClass
{
public string logFolder { get; set; } = "Log";
...
}
public OptionClass options { get; set; } = new OptionClass();
public class OptionClass
{
public bool showGraphics { get; set; } = true;
...
}
public MqttSpSetupClass MqttSpSetup { get; set; } = new MqttSpSetupClass();
public class MqttSpSetupClass
{
public string strAddress { get; set; } = "localhost";
...
}
}
so I want reflection to cycle on all member of each inner class
PropertyInfo[] props_Outer = typeof(IoAppPreferences).GetProperties();
int counter = 0;
foreach (PropertyInfo prop_Upper in props_Outer)
{
var sName_Outer = prop_Upper.Name;
var val_Outer = props_Outer.GetValue(counter ++);
PropertyInfo[] properties_Inner;
switch (sName_Outer.ToUpper())
{
case "DIMS": properties_Inner = typeof(IoAppPreferences.DimsClass).GetProperties(); break;
...
}
foreach (PropertyInfo prop_Inner in properties_Inner)
{
var sName = prop_Inner.Name;
//prefs.function
var sVal = prop_Inner.GetValue(val_Outer);<------ERROR
switch (prop_Inner.Name.ToUpper())
{
...
}
}
so I get an error where I put the arrow. And the reason is that val_Outer is FunctionClass function while if I hardcode prefs.function it is ok. Of course I can put a switch per each one but my question is: is there a better way to solve it?
I have seen this solution but can’t fit to my needs
Thanks
Patrick
>Solution :
The reason why you are getting an error at the line var sVal = prop_Inner.GetValue(val_Outer); is that val_Outer is an instance of the FunctionClass, not the inner class you are trying to get the value from.
To access the inner class instance, you need to get the value of the property from the outer class instance, and then access the property value of the inner class instance.
Here is an updated version of your code that uses nested loops to iterate over the properties of the inner classes:
Preferences prefs = new Preferences();
PropertyInfo[] props_Outer = typeof(Preferences).GetProperties();
foreach (PropertyInfo prop_Outer in props_Outer)
{
// Get the value of the outer property
var val_Outer = prop_Outer.GetValue(prefs);
// Check if the property is an inner class
if (prop_Outer.PropertyType.IsClass && prop_Outer.PropertyType.Namespace == typeof(Preferences).Namespace)
{
// Iterate over the properties of the inner class
PropertyInfo[] properties_Inner = prop_Outer.PropertyType.GetProperties();
foreach (PropertyInfo prop_Inner in properties_Inner)
{
// Get the value of the inner property
var sVal = prop_Inner.GetValue(val_Outer);
// Do something with the property value
Console.WriteLine($"Property Name: {prop_Outer.Name}.{prop_Inner.Name}, Value: {sVal}");
}
}
}
In this version of the code, we use a nested loop to first iterate over the outer class properties, and then for each outer property, we check if its type is an inner class. If it is, we iterate over the properties of the inner class and get the value of each property using the GetValue method.
Note that we use the prop_Outer.PropertyType.Namespace property to check if the type of the outer property is in the same namespace as the Preferences class. This is to ensure that we only iterate over the properties of the inner classes and not any other classes in the same namespace.