c# - iterate through instances of a class inside itself -


(i didn't create this, must work this)

class person{    public string name {     get;     private set;   }    public static person p1 = new person("name1");   public static person a1 = new person("aname1");   public static person zx1 = new person("namezx");   public static person p2 = new person("name2");   .   .   public static person p20 = new person("name20");     } 

i need iterate through of these instances, think involves reflection i'm not sure how to.

@selman22, thanks, must loop through these persons

forach(var p in persons) { } 

however p fieldinfo object, how change person (casting isn't working)

you can fields using reflection this:

var persons = typeof(person)              .getfields(bindingflags.public | bindingflags.static)              .where(x => x.name.startswith("p")); 

or:

var persons = typeof(person)               .getfields(bindingflags.public | bindingflags.static)               .where(x => x.getvalue(null) person);  foreach(var p in persons) {     var currentperson = p.getvalue(null) person;     ... } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -