7

My code invokes method using reflection:

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

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?

Val
  • 629
  • 1
  • 6
  • 12

2 Answers2

11

From MSDN on 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.

Edit:

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).

MerickOWA
  • 7,453
  • 1
  • 35
  • 56
  • Thanks! You saved me hours of failed attempts :) – Val Sep 02 '11 at 18:07
  • @MerickOWA, any workaround? any way to trust my apps? `Reflection.Emit`? – Shimmy Weitzhandler Jan 27 '12 at 10:02
  • @Shimmy I did some more testing and the only work around is to use Silverlight 5 with elevated privileges either in-browers or out-of-browser. Silverlight 4 elevated privileges does not allow reflection. I'll update the answer to reflect this since Silverlight 5 has been released. – MerickOWA Jan 27 '12 at 15:28
  • I'm on SL 5. I'll make a research on elevated trusts. – Shimmy Weitzhandler Jan 28 '12 at 20:33
  • Just FYI, the ability to get at private members with elevated privileges is buggy on Mac OSX. I've had to work around an issue where code in a leaf class *can* access the private member but the exact same code in the leaf's parent cannot... it is bizarre. – aggieNick02 Jun 18 '13 at 14:36
  • @MerickOWA. Hi Merick, I am trying to access an internal member on ManualResetEvent class (SafeWaitHandle) but I get FIeldAccessException. I am running SL5 with elevated permissions. What exactly did you mean by "DOES NOT WORK in-browser using generated test page"? I replaced the webpage with a new one and made that as the startup page but it is till the same. Can you provide more information. Thx – user559788 Nov 20 '13 at 05:32
  • @user559788 just what it says, it wont work. You need to associate your silverlight application with a local asp.net website project, and launch the website instead. – MerickOWA Nov 20 '13 at 19:02
3

If you need access to non-public members, you can do this using the LambdaExpression. I wrote this article that explains in detail why it works.

Vyacheslav Volkov
  • 4,592
  • 1
  • 20
  • 20
  • LamdaExpression doesn't work for SL 5 .NET internal methods marked with SecuritySafeCriticalAttribute. Have you tested your solution? – yar_shukan Aug 21 '14 at 14:07
  • Unfortunately, this solution also does not work for internal methods with `SecuritySafeCriticalAttribute` attribute. – Vyacheslav Volkov Aug 21 '14 at 14:11