Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / badimageformatexception.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:  BadImageFormatException
9 **
10 **
11 ** Purpose: Exception to an invalid dll or executable format.
12 **
13 ** 
14 ===========================================================*/
15 // <OWNER>Microsoft</OWNER>
16
17 namespace System {
18     
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;
25
26     [System.Runtime.InteropServices.ComVisible(true)]
27     [Serializable]
28     public class BadImageFormatException : SystemException {
29
30         private String _fileName;  // The name of the corrupt PE file.
31         private String _fusionLog;  // fusion log (when applicable)
32
33         public BadImageFormatException() 
34             : base(Environment.GetResourceString("Arg_BadImageFormatException")) {
35             SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
36         }
37     
38         public BadImageFormatException(String message) 
39             : base(message) {
40             SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
41         }
42         
43         public BadImageFormatException(String message, Exception inner) 
44             : base(message, inner) {
45             SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
46         }
47
48         public BadImageFormatException(String message, String fileName) : base(message)
49         {
50             SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
51             _fileName = fileName;
52         }
53
54         public BadImageFormatException(String message, String fileName, Exception inner) 
55             : base(message, inner) {
56             SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
57             _fileName = fileName;
58         }
59
60         public override String Message
61         {
62             get {
63                 SetMessageField();
64                 return _message;
65             }
66         }
67
68         private void SetMessageField()
69         {
70             if (_message == null) {
71                 if ((_fileName == null) &&
72                     (HResult == System.__HResults.COR_E_EXCEPTION))
73                     _message = Environment.GetResourceString("Arg_BadImageFormatException");
74
75                 else
76                     _message = FileLoadException.FormatFileLoadExceptionMessage(_fileName, HResult);
77             }
78         }
79
80         public String FileName {
81             get { return _fileName; }
82         }
83
84         public override String ToString()
85         {
86             String s = GetType().FullName + ": " + Message;
87
88             if (_fileName != null && _fileName.Length != 0)
89                 s += Environment.NewLine + Environment.GetResourceString("IO.FileName_Name", _fileName);
90             
91             if (InnerException != null)
92                 s = s + " ---> " + InnerException.ToString();
93
94             if (StackTrace != null)
95                 s += Environment.NewLine + StackTrace;
96 #if FEATURE_FUSION
97             try
98             {
99                 if(FusionLog!=null)
100                 {
101                     if (s==null)
102                         s=" ";
103                     s+=Environment.NewLine;
104                     s+=Environment.NewLine;
105                     s+=FusionLog;
106                 }
107             }
108             catch(SecurityException)
109             {
110             
111             }
112 #endif
113             return s;
114         }
115
116         protected BadImageFormatException(SerializationInfo info, StreamingContext context) : base(info, context) {
117             // Base class constructor will check info != null.
118
119             _fileName = info.GetString("BadImageFormat_FileName");
120             try
121             {
122                 _fusionLog = info.GetString("BadImageFormat_FusionLog");
123             }
124             catch 
125             {
126                 _fusionLog = null;
127             }
128         }
129
130         private BadImageFormatException(String fileName, String fusionLog, int hResult)
131             : base(null)
132         {
133             SetErrorCode(hResult);
134             _fileName = fileName;
135             _fusionLog=fusionLog;
136             SetMessageField();
137         }
138
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; }
144         }
145 #endif
146
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);
152
153             // Serialize data for this class
154             info.AddValue("BadImageFormat_FileName", _fileName, typeof(String));
155             try
156             {
157                 info.AddValue("BadImageFormat_FusionLog", FusionLog, typeof(String));
158             }
159             catch (SecurityException)
160             {
161             }
162
163         }
164 #endif
165     }
166 }