c# - How to loop through class properties tree? -
class classa { public classb myprop {get;set;} } class classb { public classc anotherprop {get;set;} } class classc { public string name {get;set;} }
i have object of classa type. how, reflection iterate recursively classc's name property value ?
i little sketchy on wanted accomplish. think want start classa , walk through properties , classc. have understand how recursive programming , small amount of knowledge of reflection. here modified version of code have used in past, can find here.
private void serializeobject(object obj) { type type = obj.gettype(); foreach (propertyinfo info2 in type.getproperties(bindingflags.getproperty | bindingflags.public | bindingflags.nonpublic | bindingflags.instance)) { methodinfo getmethod = info2.getgetmethod(true); if (getmethod != null) serializeobject(getmethod.invoke(obj, null)); } }
what walk through each property , uses method of each property execute property , object being returned can walk through calling same serializeobject
method.
Comments
Post a Comment