Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / mscorlib / system / badimageformatexception.cs
blob19783eb44a9e2c217ef152cb32809b83c54e0920
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 /*============================================================
7 **
8 ** Class: BadImageFormatException
9 **
11 ** Purpose: Exception to an invalid dll or executable format.
13 **
14 ===========================================================*/
15 // <OWNER>Microsoft</OWNER>
17 namespace System {
19 using System;
20 using System.Runtime.Serialization;
21 using FileLoadException = System.IO.FileLoadException;
22 using System.Security.Permissions;
23 using SecurityException = System.Security.SecurityException;
24 using System.Globalization;
26 [System.Runtime.InteropServices.ComVisible(true)]
27 [Serializable]
28 public class BadImageFormatException : SystemException {
30 private String _fileName; // The name of the corrupt PE file.
31 private String _fusionLog; // fusion log (when applicable)
33 public BadImageFormatException()
34 : base(Environment.GetResourceString("Arg_BadImageFormatException")) {
35 SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
38 public BadImageFormatException(String message)
39 : base(message) {
40 SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
43 public BadImageFormatException(String message, Exception inner)
44 : base(message, inner) {
45 SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
48 public BadImageFormatException(String message, String fileName) : base(message)
50 SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
51 _fileName = fileName;
54 public BadImageFormatException(String message, String fileName, Exception inner)
55 : base(message, inner) {
56 SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
57 _fileName = fileName;
60 public override String Message
62 get {
63 SetMessageField();
64 return _message;
68 private void SetMessageField()
70 if (_message == null) {
71 if ((_fileName == null) &&
72 (HResult == System.__HResults.COR_E_EXCEPTION))
73 _message = Environment.GetResourceString("Arg_BadImageFormatException");
75 else
76 _message = FileLoadException.FormatFileLoadExceptionMessage(_fileName, HResult);
80 public String FileName {
81 get { return _fileName; }
84 public override String ToString()
86 String s = GetType().FullName + ": " + Message;
88 if (_fileName != null && _fileName.Length != 0)
89 s += Environment.NewLine + Environment.GetResourceString("IO.FileName_Name", _fileName);
91 if (InnerException != null)
92 s = s + " ---> " + InnerException.ToString();
94 if (StackTrace != null)
95 s += Environment.NewLine + StackTrace;
96 #if FEATURE_FUSION
97 try
99 if(FusionLog!=null)
101 if (s==null)
102 s=" ";
103 s+=Environment.NewLine;
104 s+=Environment.NewLine;
105 s+=FusionLog;
108 catch(SecurityException)
112 #endif
113 return s;
116 protected BadImageFormatException(SerializationInfo info, StreamingContext context) : base(info, context) {
117 // Base class constructor will check info != null.
119 _fileName = info.GetString("BadImageFormat_FileName");
122 _fusionLog = info.GetString("BadImageFormat_FusionLog");
124 catch
126 _fusionLog = null;
130 private BadImageFormatException(String fileName, String fusionLog, int hResult)
131 : base(null)
133 SetErrorCode(hResult);
134 _fileName = fileName;
135 _fusionLog=fusionLog;
136 SetMessageField();
139 #if FEATURE_FUSION
140 public String FusionLog {
141 [System.Security.SecuritySafeCritical] // auto-generated
142 [SecurityPermissionAttribute( SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.ControlPolicy)]
143 get { return _fusionLog; }
145 #endif
147 #if FEATURE_SERIALIZATION
148 [System.Security.SecurityCritical] // auto-generated_required
149 public override void GetObjectData(SerializationInfo info, StreamingContext context) {
150 // Serialize data for our base classes. base will verify info != null.
151 base.GetObjectData(info, context);
153 // Serialize data for this class
154 info.AddValue("BadImageFormat_FileName", _fileName, typeof(String));
157 info.AddValue("BadImageFormat_FusionLog", FusionLog, typeof(String));
159 catch (SecurityException)
164 #endif