I'm creating List<ExpandoObject>
, against a List<SomeClassA>
. i.e, dynamically creating object using expadoObject, and adding properties to it. I could easily get the required output, but my solution fails if List<SomeClassA>
has some complex properties, i.e, Address
field, etc. (for sample example
class SomeClassA
{
string Name { get; set; }
string Age { get; set; }
Address address { get; set; }
}
class Address
{
string house { get; set; }
string city { get; set; }
}
Which means for any item in List<SomeClassA>
, there must me a property like address.house
. Since i'm in a loop, and property to be mapped can be either simple, or complex (like address). To handle this, i applied a check to know if it's a nested property, and created new expanoObject as dynamic, instead of simple property. something like:
//assume:
string name = classAobj.Name; string address = classAobj.Address;
dynamic dynObj = new ExpandoObject();
var prop = dynObj as IDictionary<string, object>;
in loop, if the property is simple (eg, name):
prop[name] = classAobj.GetType().GetProperty("Name").GetValue(classAobj);
if property is complex (eg, address):
prop[address] = new ExpandoObject() as dynamic;
//then add its properties. but can't
the point where i'm stuck is, i'm unable to get reference of this point: prop[address]
Question is: how can i access this point and add properties to it dynamically, (i.e, house, city).
PropertyInfro x = prop.GetType().GetProperty(address);
or FieldInfo f = prop.GetType().GetField(address);
returns null. While i can see it as new ExpandoObject
in debugging mode.
Question Summary: How can i refer to dynamically created nested ExpandoObject, and add properties to it. the way i am adding simple properties to outer object. I'd been through several SO posts, but nothing helped as scenario was always different.