您现在的位置是:首页 > 电脑 > 

FieldInfo.GetValue为私有成员返回null,而调试器指示字段为非null?(FieldInfo.GetValue return null for a private member while debugger indicates field is non null?)

2025-07-27 07:14:43
FieldInfo.GetValue为私有成员返回null,而调试器指示字段为非null?(FieldInfo.GetValue return null for a private member while debugger indicates field is non null?) 在C#/ .ET 4.0中,我试图通过反射检索字段值: var bar
FieldInfo.GetValue为私有成员返回null,而调试器指示字段为非null?(FieldInfo.GetValue return null for a private member while debugger indicates field is non null?)

在C#/ .ET 4.0中,我试图通过反射检索字段值:

var bar = foo.GetType() .GetField("_myField", BindingFlags.Instance | ) .GetValue(foo)

我对这种情况感到有些困惑。 返回的值为null ,但是,字段(通过调试器观察时)不为null。 更令人费解的是,上面的代码适用于其他对象属性。

唯一奇怪的方面是两个标志IsSecurityCritical和IsSecuritySafeCritical是true ,但我甚至不确定它实际上与情况有关。

我最终会遇到一个小的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 | ) .GetValue(app.Request); // at this point 'rawContent' is null, while debugger indicates it is not. } }

任何可以解释这种行为的建议?

In C# / .ET 4.0 I am trying to retrieve a field value through reflection with:

var bar = foo.GetType() .GetField("_myField", BindingFlags.Instance | ) .GetValue(foo)

I am a bit puzzled by the situation. The value returned is null, and yet, the field (when observed through the debugger) is not null. Even more puzzling, the code here above works for the other object properties.

The only odd aspect are the two flags IsSecurityCritical and IsSecuritySafeCritical that are true, but I am not even sure it's actually relevant to the situation.

I am ending up in such a 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 | ) .GetValue(app.Request); // at this point 'rawContent' is null, while debugger indicates it is not. } }

Any suggestion that would explain such a behavior?

最满意答案

这是由.net 4.0中的安全模型引起的,因为您正在运行一个应用程序,该应用程序可能无法完全信任。 由于该字段对安全性至关重要,因此无法通过反射访问它。

您可以在msdn上阅读一些关于: 反射的安全注意事项

This is caused by the security model in .net 4.0, as you're running an application, which is probably not running in full trust. As the field is security critical, you can't access it through reflection.

You can read a bit on the msdn about: Security Ciderati for Reflection

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/654019.html

相关标签:无
上传时间: 2023-07-28 12:47:39
留言与评论(共有 9 条评论)
本站网友 榴莲不能和什么一起吃
22分钟前 发表
FieldInfo.GetValue为私有成员返回null
本站网友 胃肠专科
23分钟前 发表
我试图通过反射检索字段值: var bar = foo.GetType() .GetField("_myField"
本站网友 小情侣的嘿咻事
23分钟前 发表
BindingFlags.Instance | ) .GetValue(app.Request); // at this point 'rawContent' is null
本站网友 嘉兴娱乐
14分钟前 发表
更令人费解的是
本站网友 天厨妙香
26分钟前 发表
该应用程序可能无法完全信任
本站网友 cmfu
8分钟前 发表
Security Ciderati for Reflection
本站网友 善良的妻子王晓兰
22分钟前 发表
while debugger indicates it is not. } } Any suggestion that would explain such a behavior? 最满意答案 这是由.net 4.0中的安全模型引起的
本站网友 建行基金定投手续费
22分钟前 发表
更令人费解的是