jeudi 23 juillet 2015

How to Add to an unknown List property of an object at runtime?

I have a 'Profile' object/class with an IList of 'Addresses', of which, I will only know their type [profile / addresses] at runtime via GetType() / GetProperties() etc., though I wish to .Add to this list e.g.:

var profile = session.Get<ProfileRecord>(1);

dynamic obj = new ExpandoObject();
obj = profile;
obj["Addresses"].Add(addressNew);

This does not work due to:

Cannot apply indexing with [] to an expression of type 'Test.Models.ProfileRecord'.

I've been looking at IDictionary, but have been unsuccessful in my attempts, nor even know if I should be heading down that path - so what is the correct way to go about this? This entire concept is new to me, so please don't over assume my capabilites ;) Many thanks in advance.





Aucun commentaire:

Enregistrer un commentaire