C# PropertyInfo.GetProperty在不应';T

C# PropertyInfo.GetProperty在不应';T,c#,wpf,reflection,properties,webbrowser-control,C#,Wpf,Reflection,Properties,Webbrowser Control,我试图获取WPF WebBrowser对象的私有属性的值。我可以在调试器中看到它有一个非空值 PropertyInfo activeXProp = Browser.GetType().GetProperty("ActiveXInstance", BindingFlags.Instance | BindingFlags.NonPublic); object activeX = activeXProp.GetValue(Browser, null); // here I get null for t

我试图获取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...
我的猜测是,我没有以正确的方式使用反射,但在这种情况下,正确的方式是什么? 该项目是运行在.NET4.6.1和Windows10上的WPF项目。
我尝试使用管理员权限运行它(向项目中添加清单文件),但没有效果。

activeX.GetType()返回的类型是System.\uu ComObject,它不支持这种反射。然而,有两个简单的解决方案

使用
动态

dynamic activeX = activeXProp.GetValue(Browser, null);
activeX.Silent = true;
dynamic
支持IDispatch COM接口(由所有ActiveX元素实现)提供的所有类型的COM反射

只是反思

对您的代码进行一个小的更改,其作用与上面的代码相同:

object activeX = activeXProp.GetValue(Browser, null);
activeX.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, activeX, new object[]{true});

这两个方法在对象上调用相同的东西,但我想第一个方法由于调用站点的缓存而随着时间的推移会更快。只有在由于某种原因无法使用
动态
时,才使用第二个选项。

您确定
activeX
null
?它不会给我空的。谢谢你尝试一下。不幸的是,它确实给了我一个空值。如果您想检查的话,这里是完整的项目:我在github上测试了您的代码,
activeX
不为空。只有下一行抛出异常。返回
null
的是
activeX.GetType().GetProperty(“Silent”)
。如果它真的
activeX
为null,则当没有创建窗口并且您尝试访问它时,它为null。