c# - Modifying the string list using reflection -
say, have class user has (string firstname, list siblings) want modify properties of user.
let's assume want replace strings b instead of a.
user : { firstname: "rager", siblings : { "stalin", "marx" } }
using reflection need read individual strings , following output object.
user : { firstname: "rbger", siblings : { "stblin", "mbrx" } }
let's consider below function
private object modifyobject(object t){ foreach(var propertyinfo in t.gettype.getproperties(){ var stringtobemodified = propertyinfo.getvalue(t,null); propertyinfo.setvalue(t, stringtobemodified.replace("a","b"),null) } }
the above code works fine when modifying firstname. dont know how modify strings in siblings.
i thought make use of 3rd property (optional index value indexed properties). looks whole property not indexed. siblings, propertyinfo.getvalue(t,null) gives 2 strings.
[0] -- stalin [1] -- marx.
can tell me how can modify above 2 strings after getting value using propertyinfo.getvalue(t,null)?
you can cast value list<string>
, update desired
eg
list<string> list = (list<string>)propertyinfo.getvalue(t,null); list[0] = list[0].replace("a","b");
above sample assuming propertyinfo of siblings of type list<string>
, may adjust needed.
Comments
Post a Comment