无法通过反射访问内部属性

use*_*618 4 c# reflection silverlight

我有一个extension:

 public static T GetPrivatePropertyValue<T>(this object obj, string propName)
        {
            if (obj == null) throw new ArgumentNullException("obj");
            PropertyInfo pi = obj.GetType().GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (pi == null) throw new ArgumentOutOfRangeException("propName", String.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName));
            return (T)pi.GetValue(obj, null);
        }
Run Code Online (Sandbox Code Playgroud)

而我正试图用它:

menuItem.GetPrivatePropertyValue<RadMenuItem>("ParentItem");
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误:

    return (T)pi.GetValue(obj, null);
Run Code Online (Sandbox Code Playgroud)

这是:

    (T)pi.GetValue(obj, null)   'pi.GetValue(obj, null)' threw an exception of type 'System.MethodAccessException'  Telerik.Windows.Controls.RadMenuItem
Run Code Online (Sandbox Code Playgroud)

menuItem是一个Telerik.Windows.Controls.RadMenuItem类的实例

而我想要的领域是

internal RadMenuItem ParentItem
{
    get
    {
        return ItemsControl.ItemsControlFromItemContainer(this) as RadMenuItem;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的代码出了什么问题?这是因为这个属性是内部的吗?

EDIT
Run Code Online (Sandbox Code Playgroud)

这是我的堆栈跟踪:

{System.MethodAccessException: Attempt by method 'MyApp.Extensions.GetPrivatePropertyValue<System.__Canon>(System.Object, System.String)' to access method 'Telerik.Windows.Controls.RadMenuItem.get_ParentItem()' failed.
   at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, RuntimeMethodHandleInternal method, RuntimeType parent, UInt32 invocationFlags)
   at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, IRuntimeMethodInfo method, RuntimeType parent, UInt32 invocationFlags)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at MyApp.Extensions.GetPrivatePropertyValue[T](Object obj, String propName)
   at MyApp.Calendar.OnContextMenuItemClick(Object sender, RadRoutedEventArgs e)}
Run Code Online (Sandbox Code Playgroud)

也许这很重要:我的应用技术是 Silverlight 4

vcs*_*nes 6

您的代码最有可能是部分受信任的.如果您的代码仅部分信任程序集,则无法访问内部成员,即使使用反射也是如此.您未获得ReflectPermission部分信任.

通过使用反射从部分受信任的代码访问无法从正常编译代码访问的私有,受保护或内部方法.

参考

编辑:

我的应用程序的技术是Silverlight 4

是的,这有帮助.Silverlight不允许跨程序集访问非公共成员(除非其他程序集具有InternalsVisibleToAttribute.请参阅此StackOverflow问题以获取更多信息.

总结一下,在Silverlight中,您不能使用反射来访问通常无法使用编译器访问的成员.

但是,该财产正在做什么你可以自己做.只需使用:

ItemsControl.ItemsControlFromItemContainer(obj) as RadMenuItem;