Mono Class Library: System.ObjectDisposedException Overview | MembersSystem.ObjectDisposedException Constructor |
Constructs and initializes a new instance of the ObjectDisposedException class. [Edit]
|
This constructor initializes the ObjectDisposedException.ObjectName property of the new instance using objectName. The ObjectDisposedException.Message property is initialized to a system-supplied message that describes the error and includes objectname . This message takes into account the current system culture.
The ObjectDisposedException.InnerException property of the new instance is initialized to null.
Note: If objectName is null, the ObjectDisposedException.Message property contains only an error message.[Edit]
The following example displays the error message of a ObjectDisposedException instance created using this constructor.
C# Example using System; public class ExampleDisposableObject : IDisposable { public static void Main() { ExampleDisposableObject obj = new ExampleDisposableObject(); obj.Close(); try { Console.WriteLine(obj); } catch (ObjectDisposedException e) { Console.WriteLine("Caught: {0}", e.Message); } } public ExampleDisposableObject() { isDisposed = false; } ~ExampleDisposableObject() { Dispose(true); } public void Close() { Dispose(true); } public void Dispose() { Dispose(true); } public void Dispose(bool disposing) { isDisposed = true; } public override String ToString() { if(isDisposed) throw new ObjectDisposedException("ExampleDisposableObject"); else return "This is an instance of ExampleDisposableObject."; } private bool isDisposed; }The output is
Caught: Cannot access a disposed object named "ExampleDisposableObject".
Object name: "ExampleDisposableObject".
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0