c# - How to get value of unknown properties (part solved already with reflection) -


i have existing c# application modify , need loop through object unknown properties , have half solved problem reflection.

i'm trying populate dictionary property name , property value. code below , i've given description of need in between ***s

this mvc5 project

    private dictionary<string, string> storeuserdetails ()     {             var userdetails = new dictionary<string, string>();        foreach (var useritem in useritems)       {         var thetype = useritem.gettype();         var theproperties = thetype.getproperties();          foreach (var property in theproperties)         {           userdetails.add(property.name, ***value of useritem property property name***);         }       }             return userdetails;     } 

many in advance help.

this how can it. (by way, code might error out on "dictionary key not being unique" since second useritem try add same property name dictionary. might need list<keyvaluepair<string, string>>)

        foreach (var property in theproperties)         {             // gets value of property instance.             // careful of null values.             var value = property.getvalue(useritem);              userdetails.add(property.name, value == null ? null : value.tostring());         } 

and way, if you're in mvc context, take reference system.web.routing , use following snippet.

foreach (var useritem in useritems) {  // rvd provided routing framework , gives dictionary   // of object property names , values, without doing   // funky.   var useritemdictionary= new routevaluedictionary(useritem); } 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -