0

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. Debugging mode: assume 10th location is address

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.

Zeeshan
  • 2,884
  • 3
  • 28
  • 47
  • Is your question [how to add properties to a dynamic object at runtime](http://stackoverflow.com/questions/15819720/dynamically-add-c-sharp-properties-at-runtime)? If not, can you try to reformat your question a bit, as your actual question isn't very clear to me at least. – CodeCaster Dec 10 '14 at 09:17
  • so what happens when you try `prop[address].house = "foo";` ? – Selman Genç Dec 10 '14 at 09:19
  • @Selman22 compiler says `object doesn't contain definition for house....` – Zeeshan Dec 10 '14 at 09:47
  • @CodeCaster No, it's not my question. if you see in attach image, prop is an expandoobject, i created dynamically. elements from 1 to 9 are properties that i added to an expandoObject. 10th element is an expandoObject, iside another expendoObject (think of address in classA). now i want to set properties for this inner object. How can i refer to it? – Zeeshan Dec 10 '14 at 09:50

1 Answers1

0

I figured it out. instead of PropertyInfro x = prop.GetType().GetProperty(address);, simply using prop[address] was enough for reference. so, following is how it worked.

dynamic complexProp = prop[address];
var innerProp = complexProp as IDictionary<string, object>;
innerProp[house] = "some value";
innerProp[city] = "some value";
Zeeshan
  • 2,884
  • 3
  • 28
  • 47