1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System
.Runtime
.CompilerServices
;
6 using System
.Runtime
.Serialization
;
11 /// The exception that is thrown when accessing an object that was disposed.
14 [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
15 public class ObjectDisposedException
: InvalidOperationException
17 private readonly string? _objectName
;
19 // This constructor should only be called by the EE (COMPlusThrow)
20 private ObjectDisposedException() :
21 this(null, SR
.ObjectDisposed_Generic
)
25 public ObjectDisposedException(string? objectName
) :
26 this(objectName
, SR
.ObjectDisposed_Generic
)
30 public ObjectDisposedException(string? objectName
, string? message
) : base(message
)
32 HResult
= HResults
.COR_E_OBJECTDISPOSED
;
33 _objectName
= objectName
;
36 public ObjectDisposedException(string? message
, Exception
? innerException
)
37 : base(message
, innerException
)
39 HResult
= HResults
.COR_E_OBJECTDISPOSED
;
42 protected ObjectDisposedException(SerializationInfo info
, StreamingContext context
)
45 _objectName
= info
.GetString("ObjectName");
48 public override void GetObjectData(SerializationInfo info
, StreamingContext context
)
50 base.GetObjectData(info
, context
);
51 info
.AddValue("ObjectName", ObjectName
, typeof(string));
55 /// Gets the text for the message for this exception.
57 public override string Message
61 string name
= ObjectName
;
62 if (string.IsNullOrEmpty(name
))
67 string objectDisposed
= SR
.Format(SR
.ObjectDisposed_ObjectName_Name
, name
);
68 return base.Message
+ Environment
.NewLine
+ objectDisposed
;
72 public string ObjectName
=> _objectName
?? string.Empty
;