public class FieldAccessException : MemberAccessException
Object
Exception
SystemException
MemberAccessException
FieldAccessException
mscorlib
RuntimeInfrastructure
Represents the error that occurs when there is an attempt to access a field outside the scope in which access is permitted.
[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.]
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.
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
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.Accessing member -1.
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()
System Namespace
FieldAccessException Constructors
FieldAccessException() Constructor
FieldAccessException(System.String) Constructor
FieldAccessException(System.String, System.Exception) Constructor
public FieldAccessException();
Constructs and initializes a new instance of the FieldAccessException class.
This constructor initializes the System.FieldAccessException.Message property of the new instance to a system-supplied message that describes the error, such as "Attempted to access a private or protected field inside a type." This message takes into account the current system culture.The System.FieldAccessException.InnerException property of the new instance is initialized to
null
.
System.FieldAccessException Class, System Namespace
public FieldAccessException(string message);
Constructs and initializes a new instance of the FieldAccessException class.
- 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.
This constructor initializes the System.FieldAccessException.Message property of the new instance using message. If message isnull
, the System.FieldAccessException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.The System.FieldAccessException.InnerException property of the new instance is initialized to
null
.
System.FieldAccessException Class, System Namespace
public FieldAccessException(string message, Exception inner);
Constructs and initializes a new instance of the FieldAccessException class.
- 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 .
This constructor initializes the System.FieldAccessException.Message property of the new instance using message and the System.FieldAccessException.InnerException property using inner. If message isnull
, the System.FieldAccessException.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.]
System.FieldAccessException Class, System Namespace