ObjectDisposedException.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.
  4. using System.Globalization;
  5. using System.Runtime.Serialization;
  6. namespace System
  7. {
  8. /// <devdoc>
  9. /// <para> The exception that is thrown when accessing an object that was
  10. /// disposed.</para>
  11. /// </devdoc>
  12. [Serializable]
  13. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  14. public class ObjectDisposedException : InvalidOperationException
  15. {
  16. private string _objectName;
  17. // This constructor should only be called by the EE (COMPlusThrow)
  18. private ObjectDisposedException() :
  19. this(null, SR.ObjectDisposed_Generic)
  20. {
  21. }
  22. public ObjectDisposedException(string objectName) :
  23. this(objectName, SR.ObjectDisposed_Generic)
  24. {
  25. }
  26. public ObjectDisposedException(string objectName, string message) : base(message)
  27. {
  28. HResult = HResults.COR_E_OBJECTDISPOSED;
  29. _objectName = objectName;
  30. }
  31. public ObjectDisposedException(string message, Exception innerException)
  32. : base(message, innerException)
  33. {
  34. HResult = HResults.COR_E_OBJECTDISPOSED;
  35. }
  36. protected ObjectDisposedException(SerializationInfo info, StreamingContext context)
  37. : base(info, context)
  38. {
  39. _objectName = info.GetString("ObjectName");
  40. }
  41. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  42. {
  43. base.GetObjectData(info, context);
  44. info.AddValue("ObjectName", ObjectName, typeof(string));
  45. }
  46. /// <devdoc>
  47. /// <para>Gets the text for the message for this exception.</para>
  48. /// </devdoc>
  49. public override string Message
  50. {
  51. get
  52. {
  53. string name = ObjectName;
  54. if (name == null || name.Length == 0)
  55. return base.Message;
  56. string objectDisposed = SR.Format(SR.ObjectDisposed_ObjectName_Name, name);
  57. return base.Message + Environment.NewLine + objectDisposed;
  58. }
  59. }
  60. public string ObjectName
  61. {
  62. get
  63. {
  64. if (_objectName == null)
  65. {
  66. return string.Empty;
  67. }
  68. return _objectName;
  69. }
  70. }
  71. }
  72. }