RuntimeInfrastructure mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.MemberAccessException System.Runtime.InteropServices.ComVisible(true)

The following example demonstrates a scenario under which is thrown.

The following code contains a class with a public method (MyMethod). This class is compiled into a class library.

`using System; namespace TestNameSpace { public class Class1 { public Class1() { Console.WriteLine ("Constructing with public method."); } public void MyMethod () { Console.WriteLine ("Calling MyMethod."); } } }`

The following code references the class library above, and accesses TestNameSpace.Class1.MyMethod. This code is compiled into an application.

`using System; using TestNameSpace; class AppTest { public static void Main() { Class1 test = new Class1(); test.MyMethod(); } }`

The output of the application is

Constructing with public method.

Calling MyMethod.

The code for the class library is changed and recompiled so that TestNameSpace.Class1.MyMethod is no longer public. The following code changes MyMethod from public to private.

`using System; namespace TestNameSpace { public class Class1 { public Class1() { Console.WriteLine ("Constructing with private method."); } private void MyMethod () { Console.WriteLine ("Calling MyMethod."); } } }`

When the application is executed again without being recompiled, the output is

Unhandled Exception: System.MethodAccessException: TestNameSpace.Class1.MyMethod()

at AppTest.Main()

This exception is not included in the .NET for Windows Store apps or the Portable Class Library, but it is thrown by some members that are. To catch the exception in that case, write a catch statement for instead.

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.

Beginning with the net_v40_long, the common language runtime treats application code as transparent when it is run with partial trust. See Security Considerations for Reflection.

uses the HRESULT COR_E_METHODACCESS, that has the value 0x80131510.

For a list of initial property values for an instance of , see the constructors.

The exception that is thrown when there is an invalid attempt to access a method, such as accessing a private method from partially trusted code.

- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  The following table shows the initial property values for an instance of .

  Property

  Value

  A null reference (Nothing in Visual Basic).

  The localized error message string.

  Initializes a new instance of the class, setting the property of the new instance to a system-supplied message that describes the error, such as "Attempt to access the method failed." This message takes into account the current system culture.

  0
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  The content of should be understandable to the user. The caller of this constructor is required to ensure that this string has been localized for the current system culture.

  The following table shows the initial property values for an instance of .

  Property

  Value

  A null reference (Nothing in Visual Basic).

  The error message string.

  Initializes a new instance of the class with a specified error message.

  A that describes the error.
  0
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  This constructor is called during deserialization to reconstitute the exception object transmitted over a stream. For more information, see XML and Soap Serialization.

  Initializes a new instance of the class with serialized data.

  The object that holds the serialized object data.
  The contextual information about the source or destination.
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  An exception that is thrown as a direct result of a previous exception can include a reference to the previous exception in the property. The property returns the same value that is passed into the constructor, or a null reference (Nothing in Visual Basic) if the property does not supply the inner exception value to the constructor.

  The following table shows the initial property values for an instance of .

  Property

  Value

  The inner exception reference.

  The error message string.

  Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception.

  The error message that explains the reason for the exception.
  The exception that is the cause of the current exception. If the parameter is not a null reference (Nothing in Visual Basic), the current exception is raised in a catch block that handles the inner exception.
  0
0