.net - How to get base properties and fields with values using reflection to work up the class hierarchy? -
how can base class properties , fields using reflection, can work class hierarchy 1 level @ time? goal build tree display showing properties , fields values of class instance, debugger locals window. need ability lazy load each base instance, when "base" tree node expanded, properties , fields values can shown on demand.
here simple example specified. i’m sure can modify specific needs or @ least point in right direction.
you can copy , paste example console application , set few breakpoints see how works.
the code in vb.net
module module1 private class classone private _one string private _two integer private _three double public property one() string return _one end set(byval value string) _one = value end set end property public property two() integer return _two end set(byval value integer) _two = value end set end property public property three() double return _three end set(byval value double) _three = value end set end property end class private class classalpha inherits classone private _alpha string private _beta long public property alpha() string return _alpha end set(byval value string) _alpha = value end set end property public property beta() long return _beta end set(byval value long) _beta = value end set end property end class private class classcolor inherits classalpha private _red string private _blue long public property red() string return _red end set(byval value string) _red = value end set end property public property blue() long return _blue end set(byval value long) _blue = value end set end property end class sub main() dim o new classcolor() o.red = "the color red" o.blue = 14 o.alpha = "the first" o.beta = 202 o.one = "one" o.two = 2 o.three = 3.1415927 dim helper new reflectionhelper(o) dim list1 = helper.reflectproperties(helper.baseclasses(0), o) dim list2 = helper.reflectproperties(helper.baseclasses(1), o) dim list3 = helper.reflectproperties(helper.baseclasses(2), o) end sub end module public class reflectionhelper private _sourceclass object private _baseclasses new list(of type) public sub new() end sub public sub new(source object) _sourceclass = source dim t type = _sourceclass.gettype() while (t isnot nothing) _baseclasses.add(t) t = t.basetype end while end sub public readonly property baseclasses list(of type) return _baseclasses end end property public function reflectproperties(byval baseclass type, byval instance object) list(of reflectionhelperproperties) dim result new list(of reflectionhelperproperties) each prop in baseclass.getproperties(reflection.bindingflags.declaredonly or reflection.bindingflags.getproperty or reflection.bindingflags.instance or reflection.bindingflags.public) result.add(new reflectionhelperproperties() {.name = prop.name, .instancevalue = prop.getvalue(instance, nothing)}) next return result end function end class public class reflectionhelperproperties public name string public instancevalue object end class the same code in c#
using system; using system.collections.generic; namespace consoleapplication23 { class program { static void main(string[] args) { // create highest level type classcolor o = new classcolor(); o.red = "the color red"; o.blue = 14; o.alpha = "the first"; o.beta = 202; o.one = "one"; o.two = 2; o.three = 3.1415927; reflectionhelper helper = new reflectionhelper(o); list<reflectionhelperproperties> list1 = helper.reflectproperties(helper.baseclasses[0], o); list<reflectionhelperproperties> list2 = helper.reflectproperties(helper.baseclasses[1], o); list<reflectionhelperproperties> list3 = helper.reflectproperties(helper.baseclasses[2], o); } } } public class classone { private string _one; private int _two; private double _three; public string 1 { { return _one; } set { _one = value; } } public int 2 { { return _two; } set { _two = value; } } public double 3 { { return _three; } set { _three = value; } } } public class classalpha : classone { private string _alpha; private long _beta; public string alpha { { return _alpha; } set { _alpha = value; } } public long beta { { return _beta; } set { _beta = value; } } } public class classcolor : classalpha { private string _red; private long _blue; public string red { { return _red; } set { _red = value; } } public long blue { { return _blue; } set { _blue = value; } } } public class reflectionhelper { private list<type> _baseclasses = new list<type>(); public reflectionhelper() { } public reflectionhelper(object source) { // build base types list type t = source.gettype(); while ((t != null)) { _baseclasses.add(t); t = t.basetype; } } public list<type> baseclasses { { return _baseclasses; } } public list<reflectionhelperproperties> reflectproperties(type baseclass, object instance) { list<reflectionhelperproperties> result = new list<reflectionhelperproperties>(); foreach (system.reflection.propertyinfo p in baseclass.getproperties(system.reflection.bindingflags.declaredonly | system.reflection.bindingflags.getproperty | system.reflection.bindingflags.instance | system.reflection.bindingflags.public)) { result.add(new reflectionhelperproperties { name = p.name, instancevalue = p.getvalue(instance, null) }); } return result; } } public class reflectionhelperproperties { public string name; public object instancevalue; }
Comments
Post a Comment