ObjectDisposedException 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
对释放的对象执行操作时引发的异常。
public ref class ObjectDisposedException : InvalidOperationException
public class ObjectDisposedException : InvalidOperationException
[System.Serializable]
public class ObjectDisposedException : InvalidOperationException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ObjectDisposedException : InvalidOperationException
type ObjectDisposedException = class
inherit InvalidOperationException
[<System.Serializable>]
type ObjectDisposedException = class
inherit InvalidOperationException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ObjectDisposedException = class
inherit InvalidOperationException
Public Class ObjectDisposedException
Inherits InvalidOperationException
- 继承
- 继承
- 属性
示例
下面的示例演示了导致 ObjectDisposedException 引发异常的错误。
using System;
using System.IO;
public class ObjectDisposedExceptionTest
{
public static void Main()
{
MemoryStream ms = new MemoryStream(16);
ms.Close();
try
{
ms.ReadByte();
}
catch (ObjectDisposedException e)
{
Console.WriteLine("Caught: {0}", e.Message);
}
}
}
open System
open System.IO
let ms = new MemoryStream 16
ms.Close()
try
ms.ReadByte()
|> ignore
with :? ObjectDisposedException as e ->
printfn $"Caught: {e.Message}"
Imports System.IO
Public Class ObjectDisposedExceptionTest
Public Shared Sub Main()
Dim ms As New MemoryStream(16)
ms.Close()
Try
ms.ReadByte()
Catch e As ObjectDisposedException
Console.WriteLine("Caught: {0}", e.Message)
End Try
End Sub
End Class
此代码生成以下输出:
Caught:
Cannot access a closed Stream.
注解
尝试访问实现IDisposable接口或IAsyncDisposable接口的对象的成员以及该对象已被释放时,将引发 anObjectDisposedException。 通常,此异常由以下条件之一引起:
你已调用对象的
IDisposable方法(或IDisposableAsync对象DisposeDisposeAsync的方法),并且你正在尝试访问获取或设置对象的状态的实例成员。 下面的示例演示 ObjectDisposedException 了调用该方法后尝试重置计时器通知的频率时 Timer.Dispose 引发的。using System; using System.Threading; public class Example { public static void Main() { Timer t = new Timer(TimerNotification, null, 100, Timeout.Infinite); Thread.Sleep(2000); t.Dispose(); t.Change(200, 1000); Thread.Sleep(3000); } private static void TimerNotification(Object obj) { Console.WriteLine("Timer event fired at {0:F}", DateTime.Now); } } // The example displays output like the following: // Timer event fired at Monday, July 14, 2014 11:54:08 AM // // Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object. // at System.Threading.TimerQueueTimer.Change(UInt32 dueTime, UInt32 period) // at Example.Main()open System open System.Threading let timerNotification _ = printfn $"Timer event fired at {DateTime.Now:F}" let t = new Timer(timerNotification, null, 100, Timeout.Infinite) Thread.Sleep 2000 t.Dispose() t.Change(200, 1000) |> ignore Thread.Sleep 3000 // The example displays output like the following: // Timer event fired at Monday, July 14, 2014 11:54:08 AM // // Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object. // at System.Threading.TimerQueueTimer.Change(UInt32 dueTime, UInt32 period) // at <StartupCode$fs>.main()Imports System.Threading Module Example Public Sub Main() Dim t As New Timer(AddressOf TimerNotification, Nothing, 100, Timeout.Infinite) Thread.Sleep(2000) t.Dispose() t.Change(200, 1000) Thread.Sleep(3000) End Sub Private Sub TimerNotification(obj As Object) Console.WriteLine("Timer event fired at {0:F}", Date.Now) End Sub End Module ' The example displays output like the following: ' Timer event fired at Monday, July 14, 2014 11:54:08 AM ' ' Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object. ' at System.Threading.TimerQueueTimer.Change(UInt32 dueTime, UInt32 period) ' at Example.Main()你已调用对象的
Close方法,并且正在尝试访问获取或设置对象的状态的实例成员。 通常,该方法Close提供该方法的类型的公共实现 IDisposable.Dispose 。 这同样适用于CloseAsync和<xref:System.IAsyncDisposable.DisposeAsync*?displayProperty=nameWithType>。已多次调用对象的
Dispose或DisposeAsync方法。 通常,这不会引发异常。 但是,根据类型实现 IDisposable.Dispose 方式, IAsyncDisposable.DisposeAsync或者它可能不允许对该方法进行多次调用。
在大多数情况下,此异常由开发人员错误导致。 通常,应该通过重新验证对象来更正错误,而不是在块中 try/catch 处理错误。
构造函数
| 名称 | 说明 |
|---|---|
| ObjectDisposedException(SerializationInfo, StreamingContext) |
已过时.
使用序列化的数据初始化 ObjectDisposedException 类的新实例。 |
| ObjectDisposedException(String, Exception) |
使用指定的错误消息和对作为此异常原因的内部异常的引用初始化 ObjectDisposedException 类的新实例。 |
| ObjectDisposedException(String, String) |
使用指定的对象名称和消息初始化类的新实例 ObjectDisposedException 。 |
| ObjectDisposedException(String) |
使用包含已释放对象名称的字符串初始化类的新实例 ObjectDisposedException 。 |
属性
| 名称 | 说明 |
|---|---|
| Data |
获取键/值对的集合,这些键/值对提供有关异常的其他用户定义的信息。 (继承自 Exception) |
| HelpLink |
获取或设置与此异常关联的帮助文件的链接。 (继承自 Exception) |
| HResult |
获取或设置 HRESULT,它是分配给特定异常的编码数值。 (继承自 Exception) |
| InnerException |
Exception获取导致当前异常的实例。 (继承自 Exception) |
| Message |
获取描述错误的消息。 |
| ObjectName |
获取已释放对象的名称。 |
| Source |
获取或设置导致错误的应用程序或对象的名称。 (继承自 Exception) |
| StackTrace |
获取调用堆栈上即时帧的字符串表示形式。 (继承自 Exception) |
| TargetSite |
获取引发当前异常的方法。 (继承自 Exception) |
方法
| 名称 | 说明 |
|---|---|
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetBaseException() |
在派生类中重写时,返回 Exception 一个或多个后续异常的根本原因。 (继承自 Exception) |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
已过时.
SerializationInfo检索具有参数名称和附加异常信息的对象。 |
| GetType() |
获取当前实例的运行时类型。 (继承自 Exception) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| ThrowIf(Boolean, Object) |
ObjectDisposedException如果指定 |
| ThrowIf(Boolean, Type) |
ObjectDisposedException如果指定 |
| ToString() |
创建并返回当前异常的字符串表示形式。 (继承自 Exception) |
活动
| 名称 | 说明 |
|---|---|
| SerializeObjectState |
已过时.
序列化异常以创建包含有关异常的序列化数据的异常状态对象时发生。 (继承自 Exception) |