MethodAccessException with Windows在Windows Phone 7上 [英] MethodAccessException with Reflection on Windows Phone 7

查看:164
本文介绍了MethodAccessException with Windows在Windows Phone 7上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用反射的过程中,我最近到达了我想要访问的对象(实际上是一个对象的静态实例)。



对象本身由内部类定义,因此没有其他方式可以访问它。我可以通过 Instance 属性访问静态实例,而不是直接获取参数化的构造函数。在反思的帮助下,我也可以获取此属性并将其设置为 PropertyInfo 实例 - 正确检测。



但是,我无法通过 GetValue 获取属性的值(因为它通过 get_Instance()调用内部类本身),并将其设置为一个对象,因为我得到一个 MethodAccessException



内部类是标记为 SecuritySafeCritical ,但我确实相信不应该是问题。



任何关于为什么的想法我得到这个例外?

解决方案

看看 MethodAccessException



它说



在以下情况下抛出此异常:

  *私人,受保护或内部的我从正常编译代码无法访问的方法通过使用反射从部分可信代码访问。 
*透明代码访问安全关键的方法。
*类库中方法的访问级别已更改,引用库的一个或多个程序集尚未重新编译。

检查程序集是否正确编译,并且应用程序具有适当的权限。


While working with reflection, I recently got to the point where I wanted to access an object (in fact, a static instance of an object).

The object itself is defined by an internal class, therefore there is no other way to access it. Instead of directly getting a parametrized constructor, I can access a static instance via the Instance property. With the help of reflection, I am also able to get this property and set it to a PropertyInfo instance - it is detected correctly.

However, I am not able to get the value of the property via GetValue (since it is called via get_Instance() in the internal class itself) and set it to an object because I am getting a MethodAccessException.

The internal class is marked with the SecuritySafeCritical, but I do believe that shouldn't be the problem.

Any ideas on why am I getting this exception?

解决方案

Have a look at MethodAccessException.

It says

This exception is thrown in situations such as the following:

*      A private, protected, or internal method that would not be accessible from normal compiled code is accessed from partially trusted code by using reflection.
*      A security-critical method is accessed from transparent code.
*      The access level of a method in a class library has changed, and one or more assemblies that reference the library have not been recompiled.

Check if the assembly is properly compiled and your application has proper privileges.

这篇关于MethodAccessException with Windows在Windows Phone 7上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆