c# - Setting a field of a class's instance with reflection -
i have following issue related reflection , have method looks :
[testmethod()] public void steamaccess() { testread = new testread(); steammap = new steammap(); // preparing parameters of csv actions a.writemessageparams.uiitemedittext = testcontext.datarow["searchquery"].tostring(); //read , execute testmethod testread.read(a, testcontext); }
this codeduitest
, steammap class (uitest map
). writemessageparams
class, real method writemessage
class allows me override string gets used tests writemessage
method, , plan make part of code more dynamically in read
method. :
a.writemessageparams.uiitemedittext = testcontext.datarow["searchquery"].tostring();
my problem happens in testread.read context follows :
when method running have access actions respective instance ( a
in case ) , if supposed have use a.writemessageparams.uiitemedittext
context know it, how info isn't problem, problem how make mentioned code run dynamically have tried :
/* i've done because know each method supposed end params, example method called writemessage, it's class called writemessageparams*/ public void read(object obj, testcontext testcontext) { //simplified code //trying access/get current instance's writemessageparam class object testobj = obj.gettype().getmember(submethod.code + "param"); //null messagebox.show(testobj.gettype().tostring()); // trying access uiitemedittext field ( declared public) , modify accordingly fieldinfo submethodfield = testobj.gettype().getfield("uiitemedittext"); submethodfield.setvalue(testobj,testcontext.datarow[submethod.csvcolumn].tostring()); }
i've had read on article , tried few things https://msdn.microsoft.com/en-us/library/6z33zd7h%28v=vs.110%29.aspx
my problem have object of instance , try access object's class , modify class's field .
i'd appreciate help, thanks
edit 1: how class i'm trying access looks :
public partial class steammap { //simplified classes/methods interest me public virtual writemessageparams writemessageparams { { if ((this.mwritemessageparams == null)) { this.mwritemessageparams = new writemessageparams(); } return this.mwritemessageparams; } } public class writemessageparams { #region fields /// <summary> /// type 'test' in text box /// </summary> public string uiitemedittext = "test"; #endregion } }
edit 2 - i've tried using getnestedtype, still no success....
object testobj = obj.gettype().getnestedtype("writemessageparams",bindingflags.public); messagebox.show(testobj.gettype().tostring());
if understand you, have class
public partial class steammap { private writemessageparams mwritemessageparams ; public virtual writemessageparams writemessageparams1 { { if ((this.mwritemessageparams == null)) { this.mwritemessageparams = new writemessageparams(); } return this.mwritemessageparams; } } public class writemessageparams { public string uiitemedittext = "test"; } }
(your code doesn't compile because have writemessageparams both class , property, have changed property writemessageparams1)
and want change uiitemedittext, can like
public void updateui(object obj, string newvalue) { var property = obj.gettype().getproperty("writemessageparams1"); var writemessageparams1 = property.getvalue(obj); var uifld = wp.gettype().getfield("uiitemedittext"); uifld.setvalue(writemessageparams1, newvalue); }
which can called like
steammap sm = new steammap(); write(sm, "hello");
the key use .getproperty
property , .getfield
field.
Comments
Post a Comment