System.MethodAccessException Class

public class MethodAccessException : MemberAccessException

Base Types

Object
  Exception
    SystemException
      MemberAccessException
        MethodAccessException

Assembly

mscorlib

Library

RuntimeInfrastructure

Summary

Represents the error that occurs when there is an attempt to access a method outside the scope in which access is permitted.

Description

[Note: This exception is thrown when the access level of a method in a class library is changed, and one or more assemblies referencing the library have not been recompiled. This exception is also thrown when an attempt to invoke a method via reflection fails because the caller does not have the required permissions.]

Example

The following example demonstrates a scenario under which MethodAccessException 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()

See Also

System Namespace

Members

MethodAccessException Constructors

MethodAccessException() Constructor
MethodAccessException(System.String) Constructor
MethodAccessException(System.String, System.Exception) Constructor


MethodAccessException() Constructor

public MethodAccessException();

Summary

Constructs and initializes a new instance of the MethodAccessException class.

Description

This constructor initializes the System.MethodAccessException.Message 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.

The System.MethodAccessException.InnerException property of the new instance is initialized to null .

See Also

System.MethodAccessException Class, System Namespace

MethodAccessException(System.String) Constructor

public MethodAccessException(string message);

Summary

Constructs and initializes a new instance of the MethodAccessException class with a specified error message.

Parameters

message
A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.

Description

This constructor initializes the System.MethodAccessException.Message property of the new instance using message. If message is null , the System.MethodAccessException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.

The System.MethodAccessException.InnerException property of the new instance is initialized to null .

See Also

System.MethodAccessException Class, System Namespace

MethodAccessException(System.String, System.Exception) Constructor

public MethodAccessException(string message, Exception inner);

Summary

Constructs and initializes a new instance of the MethodAccessException class with a specified error message and a reference to the inner exception that is the cause of the current exception.

Parameters

message
A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
inner
An instance of Exception that is the cause of the current exception. If inner is not a null reference, the current exception was raised in a catch block handling inner .

Description

This constructor initializes the System.MethodAccessException.Message property of the new instance using message and the System.MethodAccessException.InnerException property using inner. If message is null , the System.MethodAccessException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.

[Note: For more information on inner exceptions, see System.Exception.InnerException.]

See Also

System.MethodAccessException Class, System Namespace