In C # /. NET 4.0, I am trying to get the value of a field through reflection using
var bar = foo.GetType()
.GetField("_myField", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(foo)
I am a little puzzled by the situation. The return value null, and yet the field (if it is observed through the debugger) is not null. Even more perplexing, the code above works for other properties of the object.
The only odd aspect is the two flags IsSecurityCriticaland IsSecuritySafeCritical, which true, but I'm not even sure that it really is relevant to the situation.
I get into this situation with a small HttpModule.
public class MyModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += BeginRequest;
}
void BeginRequest(object sender, EventArgs e)
{
var app = (HttpApplication)sender;
var rawContent = typeof(HttpRequest)
.GetField("_rawContent", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(app.Request);
}
}
Any suggestion that explains this behavior?