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 /*============================================================
9 ** Purpose: Exception to an invalid dll or executable format.
12 ===========================================================*/
15 using System
.Runtime
.Serialization
;
20 [System
.Runtime
.CompilerServices
.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
21 public partial class BadImageFormatException
: SystemException
23 private readonly string? _fileName
; // The name of the corrupt PE file.
24 private readonly string? _fusionLog
; // fusion log (when applicable)
26 public BadImageFormatException()
27 : base(SR
.Arg_BadImageFormatException
)
29 HResult
= HResults
.COR_E_BADIMAGEFORMAT
;
32 public BadImageFormatException(string? message
)
35 HResult
= HResults
.COR_E_BADIMAGEFORMAT
;
38 public BadImageFormatException(string? message
, Exception
? inner
)
39 : base(message
, inner
)
41 HResult
= HResults
.COR_E_BADIMAGEFORMAT
;
44 public BadImageFormatException(string? message
, string? fileName
) : base(message
)
46 HResult
= HResults
.COR_E_BADIMAGEFORMAT
;
50 public BadImageFormatException(string? message
, string? fileName
, Exception
? inner
)
51 : base(message
, inner
)
53 HResult
= HResults
.COR_E_BADIMAGEFORMAT
;
57 protected BadImageFormatException(SerializationInfo info
, StreamingContext context
)
60 _fileName
= info
.GetString("BadImageFormat_FileName");
61 _fusionLog
= info
.GetString("BadImageFormat_FusionLog");
64 public override void GetObjectData(SerializationInfo info
, StreamingContext context
)
66 base.GetObjectData(info
, context
);
67 info
.AddValue("BadImageFormat_FileName", _fileName
, typeof(string));
68 info
.AddValue("BadImageFormat_FusionLog", _fusionLog
, typeof(string));
71 public override string Message
80 private void SetMessageField()
84 if ((_fileName
== null) &&
85 (HResult
== HResults
.COR_E_EXCEPTION
))
86 _message
= SR
.Arg_BadImageFormatException
;
88 _message
= FileLoadException
.FormatFileLoadExceptionMessage(_fileName
, HResult
);
92 public string? FileName
=> _fileName
;
94 public override string ToString()
96 string s
= GetType().ToString() + ": " + Message
;
98 if (!string.IsNullOrEmpty(_fileName
))
99 s
+= Environment
.NewLine
+ SR
.Format(SR
.IO_FileName_Name
, _fileName
);
101 if (InnerException
!= null)
102 s
= s
+ InnerExceptionPrefix
+ InnerException
.ToString();
104 if (StackTrace
!= null)
105 s
+= Environment
.NewLine
+ StackTrace
;
107 if (_fusionLog
!= null)
110 s
+= Environment
.NewLine
;
111 s
+= Environment
.NewLine
;
118 public string? FusionLog
=> _fusionLog
;