访问使用C#/ Silverlight应用程序反射私有方法 [英] Access to private method using reflection in C#/Silverlight applications

查看:126
本文介绍了访问使用C#/ Silverlight应用程序反射私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码调用方法使用反射:

My code invokes method using reflection:

        scoringType.InvokeMember("scoringClient_ScorePostsCompleted",
            BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
            null, scoringInstance,
            new object[] { sArg, eArg });

其中scoringInstance是一个模型视图类的一个实例。该方法是私有的,但我用BindingFlags.NonPublic可,所以,我应该能够访问它,但我不能 - 我得到MethodAccessException异常:由法尝试...访问方法...失败
谷歌似乎没有有一个答案。 ?你有什么想法如何通过任何机会来解决它。

where scoringInstance is an instance of a ModelView class. The method is private, but I use BindingFlags.NonPublic, so, i should be able to access it, but I cannot - I get MethodAccessException exception: "Attempt by method ... to access method ... failed." Google doesnt seem to have an answer. Do you have any idea how to fix it by any chance?

推荐答案

从的 MSDN上的Silverlight

在Silverlight中,您不能使用反射来访问私有类型和
的成员。如果一个类型或成员的访问级别,将防止你在静态编译的代码访问它
,你不能访问它通过使用反射动态

In Silverlight, you cannot use reflection to access private types and members. If the access level of a type or member would prevent you from accessing it in statically compiled code, you cannot access it dynamically by using reflection.

编辑:

5的Silverlight现在这样只允许如果你使用提升的权限或者外运行私有成员反射-browser或在浏览器(在浏览器中使用生成的测试页面不工作)。

Silverlight 5 now does allow for reflection of private members ONLY if you're running with elevated privileges either out-of-browser or in-browser (in-browser using the generated test page DOES NOT WORK).

这篇关于访问使用C#/ Silverlight应用程序反射私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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