Fix pragma warning restore (dotnet/coreclr#26389)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / ObjectDisposedException.cs
blob09d43ed6667b20c6536e043acfca73abe7f3c6cd
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;
8 namespace System
10 /// <summary>
11 /// The exception that is thrown when accessing an object that was disposed.
12 /// </summary>
13 [Serializable]
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)
43 : base(info, 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));
54 /// <summary>
55 /// Gets the text for the message for this exception.
56 /// </summary>
57 public override string Message
59 get
61 string name = ObjectName;
62 if (string.IsNullOrEmpty(name))
64 return base.Message;
67 string objectDisposed = SR.Format(SR.ObjectDisposed_ObjectName_Name, name);
68 return base.Message + Environment.NewLine + objectDisposed;
72 public string ObjectName => _objectName ?? string.Empty;