Mono Class Library: System Namespace

System.FieldAccessException Class

Represents the error that occurs when there is an attempt to access a field outside the scope in which access is permitted. [Edit]

See Also: FieldAccessException Members

System.Object
     System.Exception
          System.SystemException
               System.MemberAccessException
                    System.FieldAccessException

[System.Runtime.InteropServices.ComVisible(true)]
public class FieldAccessException : MemberAccessException

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Remarks

Note: 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.
[Edit]

Example

The following example demonstrates a scenario under which FieldAccessException is thrown.

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

C# Example
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.

C# Example
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.

C# Example
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()

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0