BadImageFormatException.cs 3.8 KB

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