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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -