dynamic - Dynamically Flat C# object class List of properties at Runtime -


i've class that:

public class {     private string id;              // generated on server     private datetime timestamp;     private int trash;     private humanity.feedtypeenum feedtype     private list<property> properties; // ... 

where property is:

public class property {     private humanity.propertytypeenum type;     private string key;     private object value; //... 

i'd build dynamic object flat list<property> properties a's field raw properties. example:

a = new a(); a.id = "id"; a.timestamp = datetime.now; a.trash = 2; a.feedtype = humanity.feedtypeenum.mail; a.properties = new list<property>() {     new property()     {         type = humanity.propertytypeenum.string,         key = "name"         value = "file1.pdf"     },     new property()     {         type = humanity.propertytypeenum.timestamp,         key = "creationdate",         value = datetime.now     } } 

as i've commented i'd flat a object in order access properties as:

string name = a.name; datetime creationdate = a.creationdate; a.name = "othername"; a.creationdate = creationdate.adddays(1); 

i've achieved using reflection. however, i'm figuring out it's best option using expandoobject.

the question is, how can using expandoobject class?

you can want extending dynamicobject class:

class : system.dynamic.dynamicobject {     // other members     public list<property> properties;      private readonly dictionary<string, object> _membersdict = new dictionary<string, object>();      public override bool trygetmember(system.dynamic.getmemberbinder binder, out object result)     {         result = null;         if (!_membersdict.containskey(binder.name))             return false;          result = _membersdict[binder.name];         return true;     }      public override bool trysetmember(system.dynamic.setmemberbinder binder, object value)     {         if (!_membersdict.containskey(binder.name))             return false;          _membersdict[binder.name] = value;         return true;     }      public void createproperties()     {         foreach (property prop in properties)         {             if (!_membersdict.containskey(prop.key))             {                 _membersdict.add(prop.key, prop.value);             }         }     } } 

then use that:

a = new a(); ///////// a.properties = new list<property>() {     new property()     {         type = humanity.propertytypeenum.string, // property useless, value field has type, if want can add logic around property ensure value same type         key = "name"         value = "file1.pdf"     },     new property()     {         type = humanity.propertytypeenum.timestamp,         key = "creationdate",         value = datetime.now     } } a.createproperties(); dynamic dyna = a; dyna.name = "value1"; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -