[System] Process.WaitForExit now triggers event Exited.
[mono.git] / mcs / class / corlib / System / BadImageFormatException.cs
1 //
2 // System.BadImageFormatException.cs
3 //
4 // Authors:
5 //   Sean MacIsaac (macisaac@ximian.com)
6 //   Duncan Mak (duncan@ximian.com)
7 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //
9 // (C) 2001 Ximian, Inc.
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Globalization;
33 using System.Runtime.Serialization;
34 using System.Security.Permissions;
35 using System.Runtime.InteropServices;
36 using System.Text;
37
38 namespace System
39 {
40         [Serializable]
41         [ComVisible (true)]
42         public class BadImageFormatException : SystemException
43         {
44                 const int Result = unchecked ((int)0x8007000B);
45
46                 // Fields
47                 private string fileName;
48                 private string fusionLog;
49
50                 // Constructors
51                 public BadImageFormatException ()
52                         : base (Locale.GetText ("Format of the executable (.exe) or library (.dll) is invalid."))
53                 {
54                         HResult = Result;
55                 }
56
57                 public BadImageFormatException (string message)
58                         : base (message)
59                 {
60                         HResult = Result;
61                 }
62
63                 protected BadImageFormatException (SerializationInfo info, StreamingContext context)
64                         : base (info, context)
65                 {
66                         fileName = info.GetString ("BadImageFormat_FileName");
67                         fusionLog = info.GetString ("BadImageFormat_FusionLog");
68                 }
69
70                 public BadImageFormatException (string message, Exception inner)
71                         : base (message, inner)
72                 {
73                         HResult = Result;
74                 }
75
76                 public BadImageFormatException (string message, string fileName)
77                         : base (message)
78                 {
79                         this.fileName = fileName;
80                         HResult = Result;
81                 }
82
83                 public BadImageFormatException (string message, string fileName, Exception inner)
84                         : base (message, inner)
85                 {
86                         this.fileName = fileName;
87                         HResult = Result;
88                 }
89
90                 // Properties
91                 public override string Message
92                 {
93                         get {
94                                 if (base.message == null) {
95                                         return string.Format (
96                                                 "Could not load file or assembly '{0}' or one of"
97                                                 + " its dependencies. An attempt was made to load"
98                                                 + " a program with an incorrect format.", fileName);
99                                 }
100                                 return base.Message;
101                         }
102                 }
103
104                 public string FileName
105                 {
106                         get { return fileName; }
107                 }
108
109                 [MonoTODO ("Probably not entirely correct. fusionLog needs to be set somehow (we are probably missing internal constuctor)")]
110                 public string FusionLog {
111                         // note: MS runtime throws a SecurityException when the Exception is created
112                         // but a FileLoadException once the exception as been thrown. Mono always
113                         // throw a SecurityException in both case (anyway fusionLog is currently empty)
114                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
115                         get { return fusionLog; }
116                 }
117
118                 // Methods
119                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
120                 {
121                         base.GetObjectData (info, context);
122                         info.AddValue ("BadImageFormat_FileName", fileName);
123                         info.AddValue ("BadImageFormat_FusionLog", fusionLog);
124                 }
125
126                 public override string ToString ()
127                 {
128                         StringBuilder sb = new StringBuilder (GetType ().FullName);
129                         sb.AppendFormat (": {0}", Message);
130
131                         if (fileName != null && fileName.Length > 0) {
132                                 sb.Append (Environment.NewLine);
133                                 sb.AppendFormat ("File name: '{0}'", fileName);
134                         }
135
136                         if (this.InnerException != null)
137                                 sb.AppendFormat (" ---> {0}", InnerException);
138
139                         if (this.StackTrace != null) {
140                                 sb.Append (Environment.NewLine);
141                                 sb.Append (StackTrace);
142                         }
143
144                         return sb.ToString ();
145                 }
146         }
147 }