【发布时间】:2017-01-03 05:36:19
【问题描述】:
我正在尝试获取 WPF WebBrowser 对象的私有属性的值。我可以在调试器中看到它有一个非空值。
PropertyInfo activeXProp = Browser.GetType().GetProperty("ActiveXInstance", BindingFlags.Instance | BindingFlags.NonPublic);
object activeX = activeXProp.GetValue(Browser, null); // here I get null for the activeX value
activeX.GetType().GetProperty("Silent").SetValue(activeX, true); // and here it crashes for calling a method on a null reference...
我的猜测是我没有以正确的方式使用反射,但在这种情况下正确的方法是什么? 该项目是在 .NET 4.6.1 和 Windows 10 上运行的 WPF 项目。 我尝试使用管理员权限运行它(将清单文件添加到项目中),但没有任何区别。
【问题讨论】:
-
你确定
activeX是null吗?它不会给我 null。 -
感谢您试一试。不幸的是,它确实给了我一个空值。如果您想查看,这里是完整的项目:github.com/zskolbay/WpfBrowserTest
-
我在 github 上测试了你的代码,
activeX不为空。只有下一行会引发异常。 -
返回
null的是activeX.GetType().GetProperty("Silent")。 -
如果真的
activeX为null,则在没有创建窗口并且您尝试访问它时为null。
标签: c# wpf reflection properties webbrowser-control