c# - Get property value in List<> instead of [0],[1] -


i using dynamic class generate list, in property ;ist dont names in static class [0],1,[2], ect. need these names properties.

anybody knows (probably) simple answer?

here code

update 1:

list<string> properties = new list<string>();         properties.add("name");         properties.add("institutetypeid");         properties.add("city");         properties.add("id");  list<dynamicclass> dynamicclasslist = new list<dynamicclass>();              int = 0;            foreach (datarow r in _data.rows)            {                dynamicclass dynamicclass = new dynamicclass();             foreach (string property in properties)               {                 dynamicclass.property[property.tostring()] = _data.rows[i][property].tostring(); // private string randomstring(int size)             }             dynamicclasslist.add(dynamicclass); } 

my dynamic class is:

public class dynamicclass {     // property class create dynamic properties @ runtime     private dynamicproperty _property = new dynamicproperty();      public dynamicproperty property     {         { return _property; }         set { _property = value; }     } } public class dynamicproperty {     // dictionary hold dynamic property values     private dictionary<string, object> properties = new dictionary<string, object>();      // property call dynamic property in our dictionary, or "" if none found.     public object this[string name]     {                 {             if (properties.containskey(name))             {                 return properties[name];             }             return "";         }         set         {             properties[name] = value;         }     } } 

which should give me same result as:

medical data = new medical(); data.name = _data.rows[i]["name"].tostring(); data.institutetypeid = convert.toint32(_data.rows[i]["institutetypeid"].tostring()); data.city = _data.rows[i]["city"].tostring(); data.id = convert.toint32(_data.rows[i]["id"].tostring()); list.add(data); 

update 2:

public class medical {     public string name { get; set; }     public int id { get; set; }     public string city { get; set; }     public int institutetypeid { get; set; } } 

if property on runtime dynamicclasslist see dynamic (sorry dont know how upload image) this:

key   value [0]   [{id,1}] [1]   [{name, med 1}] 

while in static class correct need

key   value [id]  {1} [name] {med 1} 

enter image description here

from point of view same slide difference static see key values in key field , dynamic in [{key,value}]

any appreciated

you need use debuggertypeproxy , few specialized classes.

public class program {     private static void main(string[] args)     {         dynamicclass dynamicclass = new dynamicclass();         dynamicclass.property["test"] = 1;         dynamicclass.property["test2"] = "foo";          debugger.break();     } }   [debuggertypeproxy(typeof(dynamicclassproxy))] public class dynamicclass {     // property class create dynamic properties @ runtime     private dynamicproperty _property = new dynamicproperty();      public dynamicproperty property     {         { return _property; }         set { _property = value; }     }      [debuggerdisplay("{value}", name = "{key}")]     private class keyvaluepair     {         private string key;         private object value;          public keyvaluepair(keyvaluepair<string,object> kvp)         {             this.value = kvp.value;             this.key = kvp.key;         }     }      private class dynamicclassproxy     {         private dynamicclass _dynamicclass;         public dynamicclassproxy(dynamicclass dynamicclass)         {             _dynamicclass = dynamicclass;         }          [debuggerbrowsable(debuggerbrowsablestate.roothidden)]         public keyvaluepair[] keys         {                         {                 return _dynamicclass.property.properties.select(x => new keyvaluepair(x)).toarray();             }         }     }      public class dynamicproperty     {         //make no 1 can create these objects.         internal dynamicproperty() { }          // dictionary hold dynamic property values         internal dictionary<string, object> properties = new dictionary<string, object>();          // property call dynamic property in our dictionary, or "" if none found.         public object this[string name]         {                         {                 if (properties.containskey(name))                 {                     return properties[name];                 }                 return "";             }             set             {                 properties[name] = value;             }         }     } } 

update: remote quotes around key name add nq name property.

[debuggerdisplay("{value}", name = "{key,nq}")] private class keyvaluepair {     private object key;     private object value;      public keyvaluepair(keyvaluepair<string,object> kvp)     {         this.value = kvp.value;         this.key = kvp.key;     } } 

enter image description here


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -