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 field (myField). This class is compiled into a class library.

`using System; namespace TestNameSpace { public class Class1 { public Class1() { Console.WriteLine ("Constructing with public field"); } public int myField = -1; } }`

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

`using System; using TestNameSpace; class AppTest { public static void Main() { Class1 test = new Class1(); Console.WriteLine("Accessing member {0}.", test.myField); } }`

The output of the application is

Constructing with public field

Accessing member -1.

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

`using System; namespace TestNameSpace { public class Class1 { public Class1() { Console.WriteLine ("Constructing with private field"); } private int myField = -1; } }`

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

Unhandled Exception: System.FieldAccessException: TestNameSpace.Class1.myField

at AppTest.Main()

This exception is typically thrown when the access level of a field in a class library is changed, and one or more assemblies referencing the library have not been recompiled.

uses the HRESULT COR_E_FIELDACCESS, which has the value 0x80131507.

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

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.

The exception that is thrown when there is an invalid attempt to access a private or protected field inside a class.

- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  This constructor initializes the property of the new instance to a system-supplied message that describes the error, such as "Attempted to access a field that is not accessible by the caller." This message takes the current system culture into account.

  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.

  0
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  This constructor initializes the property of the new instance by using the parameter. The content of is intended to be understood by humans. The caller of this constructor must 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 localized error message string.

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

  The error message that explains the reason for the exception.
  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 that was transmitted over a stream.

  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 should include a reference to the previous exception in the property. The property returns the same value that is passed into the constructor, or returns null if it does not supply the inner exception value to the constructor. The caller of this constructor must 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

  The inner exception reference.

  The localized 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 null, the current exception is raised in a catch block that handles the inner exception.
  0
0