More Corelib cleanup (dotnet/coreclr#26872)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / BadImageFormatException.cs
blob246fe817c7d080b505e4c72d78c6396caf282b55
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 /*============================================================
6 **
7 **
8 **
9 ** Purpose: Exception to an invalid dll or executable format.
12 ===========================================================*/
14 using System.IO;
15 using System.Runtime.Serialization;
17 namespace System
19 [Serializable]
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)
33 : base(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;
47 _fileName = fileName;
50 public BadImageFormatException(string? message, string? fileName, Exception? inner)
51 : base(message, inner)
53 HResult = HResults.COR_E_BADIMAGEFORMAT;
54 _fileName = fileName;
57 protected BadImageFormatException(SerializationInfo info, StreamingContext context)
58 : base(info, 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
73 get
75 SetMessageField();
76 return _message!;
80 private void SetMessageField()
82 if (_message == null)
84 if ((_fileName == null) &&
85 (HResult == HResults.COR_E_EXCEPTION))
86 _message = SR.Arg_BadImageFormatException;
87 else
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)
109 s ??= " ";
110 s += Environment.NewLine;
111 s += Environment.NewLine;
112 s += _fusionLog;
115 return s;
118 public string? FusionLog => _fusionLog;