c# - Get private property of a private property using reflection -
public class foo { private bar foobar {get;set;} private class bar { private string str {get;set;} public bar() {str = "some value";} } }
if i've got above , have reference foo, how can use reflection value str out foo's foobar? know there's no actual reason ever (or very few ways), figure there has way , can't figure out how accomplish it.
edited because asked wrong question in body differs correct question in title
you can use getproperty
method along nonpublic
, instance
binding flags.
assuming have instance of foo
, f
:
propertyinfo prop = typeof(foo).getproperty("foobar", bindingflags.nonpublic | bindingflags.instance); methodinfo getter = prop.getgetmethod(nonpublic: true); object bar = getter.invoke(f, null);
update:
if want access str
property, same thing on bar
object that's retrieved:
propertyinfo strproperty = bar.gettype().getproperty("str", bindingflags.nonpublic | bindingflags.instance); methodinfo strgetter = strproperty.getgetmethod(nonpublic: true); string val = (string)strgetter.invoke(bar, null);
Comments
Post a Comment