BadImageFormatException.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /*============================================================
  5. **
  6. **
  7. **
  8. ** Purpose: Exception to an invalid dll or executable format.
  9. **
  10. **
  11. ===========================================================*/
  12. using System.IO;
  13. using System.Runtime.Serialization;
  14. namespace System
  15. {
  16. [Serializable]
  17. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  18. public partial class BadImageFormatException : SystemException
  19. {
  20. private readonly string? _fileName; // The name of the corrupt PE file.
  21. private readonly string? _fusionLog; // fusion log (when applicable)
  22. public BadImageFormatException()
  23. : base(SR.Arg_BadImageFormatException)
  24. {
  25. HResult = HResults.COR_E_BADIMAGEFORMAT;
  26. }
  27. public BadImageFormatException(string? message)
  28. : base(message)
  29. {
  30. HResult = HResults.COR_E_BADIMAGEFORMAT;
  31. }
  32. public BadImageFormatException(string? message, Exception? inner)
  33. : base(message, inner)
  34. {
  35. HResult = HResults.COR_E_BADIMAGEFORMAT;
  36. }
  37. public BadImageFormatException(string? message, string? fileName) : base(message)
  38. {
  39. HResult = HResults.COR_E_BADIMAGEFORMAT;
  40. _fileName = fileName;
  41. }
  42. public BadImageFormatException(string? message, string? fileName, Exception? inner)
  43. : base(message, inner)
  44. {
  45. HResult = HResults.COR_E_BADIMAGEFORMAT;
  46. _fileName = fileName;
  47. }
  48. protected BadImageFormatException(SerializationInfo info, StreamingContext context)
  49. : base(info, context)
  50. {
  51. _fileName = info.GetString("BadImageFormat_FileName");
  52. _fusionLog = info.GetString("BadImageFormat_FusionLog");
  53. }
  54. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  55. {
  56. base.GetObjectData(info, context);
  57. info.AddValue("BadImageFormat_FileName", _fileName, typeof(string));
  58. info.AddValue("BadImageFormat_FusionLog", _fusionLog, typeof(string));
  59. }
  60. public override string Message
  61. {
  62. get
  63. {
  64. SetMessageField();
  65. return _message!;
  66. }
  67. }
  68. private void SetMessageField()
  69. {
  70. if (_message == null)
  71. {
  72. if ((_fileName == null) &&
  73. (HResult == HResults.COR_E_EXCEPTION))
  74. _message = SR.Arg_BadImageFormatException;
  75. else
  76. _message = FileLoadException.FormatFileLoadExceptionMessage(_fileName, HResult);
  77. }
  78. }
  79. public string? FileName => _fileName;
  80. public override string ToString()
  81. {
  82. string s = GetType().ToString() + ": " + Message;
  83. if (!string.IsNullOrEmpty(_fileName))
  84. s += Environment.NewLineConst + SR.Format(SR.IO_FileName_Name, _fileName);
  85. if (InnerException != null)
  86. s += InnerExceptionPrefix + InnerException.ToString();
  87. if (StackTrace != null)
  88. s += Environment.NewLineConst + StackTrace;
  89. if (_fusionLog != null)
  90. {
  91. s ??= " ";
  92. s += Environment.NewLineConst + Environment.NewLineConst + _fusionLog;
  93. }
  94. return s;
  95. }
  96. public string? FusionLog => _fusionLog;
  97. }
  98. }