I have an expando, like
dynamic x = new ExpandoObject ();
AddProperty (x, "Name", "Nick");
AddProperty (x, "Seat", "8H");
AddProperty (x, "Code", "11");
Console.WriteLine(x.Name);
Console.WriteLine (x.Seat);
Console.WriteLine (x.Code);
and I use an AddProperty method for the above, as per source found, i.e.
public static void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
{
// ExpandoObject supports IDictionary so we can extend it like this
var expandoDict = expando as IDictionary<string, object>;
if ( expandoDict.ContainsKey (propertyName) )
expandoDict[propertyName] = propertyValue;
else
expandoDict.Add (propertyName, propertyValue);
}
Now, I use this method to iterate through my Classes (and it works, even for linked classes, i.e. for properties that are defined using other classes, i.e. public anotherClass address { get; set; } etc..) and it works fine so far:
private static void IterateThrough(object source)
{
Type sourceType = source.GetType(); //Get current object type of source
//Iterate through all properties of the source object
foreach (PropertyInfo prop in sourceType.GetProperties
(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
var Type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
if (prop.PropertyType.IsClass && prop.PropertyType != typeof(string)) {
object o = prop.GetValue(source, null);
IterateThrough(o);
}
Console.WriteLine(prop.DeclaringType.Name + "." + prop.Name);
}
}
However, when I try with the expando, I get an array length error and a stack overflow eventually.
What's the error and ho do I correctly iterate through my objects, so that it can run for both 'normal' case classes as well as my expando?