Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / mscorlib / system / io / fileloadexception.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:  FileLoadException
9 **
10 ** <OWNER>[....]</OWNER>
11 ** <OWNER>[....]</OWNER>
12 **
13 **
14 ** Purpose: Exception for failure to load a file that was successfully found.
15 **
16 **
17 ===========================================================*/
18
19 using System;
20 using System.Globalization;
21 using System.Runtime.Serialization;
22 using System.Runtime.InteropServices;
23 using System.Runtime.CompilerServices;
24 using System.Security;
25 using System.Security.Permissions;
26 using System.Runtime.Versioning;
27 using SecurityException = System.Security.SecurityException;
28
29 namespace System.IO {
30
31     [Serializable]
32 [System.Runtime.InteropServices.ComVisible(true)]
33     public class FileLoadException : IOException {
34
35         private String _fileName;   // the name of the file we could not load.
36         private String _fusionLog;  // fusion log (when applicable)
37
38         public FileLoadException() 
39             : base(Environment.GetResourceString("IO.FileLoad")) {
40             SetErrorCode(__HResults.COR_E_FILELOAD);
41         }
42     
43         public FileLoadException(String message) 
44             : base(message) {
45             SetErrorCode(__HResults.COR_E_FILELOAD);
46         }
47     
48         public FileLoadException(String message, Exception inner) 
49             : base(message, inner) {
50             SetErrorCode(__HResults.COR_E_FILELOAD);
51         }
52
53         public FileLoadException(String message, String fileName) : base(message)
54         {
55             SetErrorCode(__HResults.COR_E_FILELOAD);
56             _fileName = fileName;
57         }
58
59         public FileLoadException(String message, String fileName, Exception inner) 
60             : base(message, inner) {
61             SetErrorCode(__HResults.COR_E_FILELOAD);
62             _fileName = fileName;
63         }
64
65         public override String Message
66         {
67             get {
68                 SetMessageField();
69                 return _message;
70             }
71         }
72
73         private void SetMessageField()
74         {
75             if (_message == null)
76                 _message = FormatFileLoadExceptionMessage(_fileName, HResult);
77         }
78
79         public String FileName {
80             get { return _fileName; }
81         }
82
83 #if FEATURE_LEGACYNETCF
84         // override Data property to populate FileLoadException with Hresult
85         public override System.Collections.IDictionary Data { 
86             [System.Security.SecuritySafeCritical]
87             get {
88                 var _data = base.Data;
89                 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8 && !_data.Contains("HResult"))
90                 {
91                     _data.Add("HResult", HResult);
92                 }
93                 return _data;
94            }
95         }
96 #endif //FEATURE_LEGACYNETCF
97
98         public override String ToString()
99         {
100             String s = GetType().FullName + ": " + Message;
101
102             if (_fileName != null && _fileName.Length != 0)
103                 s += Environment.NewLine + Environment.GetResourceString("IO.FileName_Name", _fileName);
104             
105             if (InnerException != null)
106                 s = s + " ---> " + InnerException.ToString();
107
108             if (StackTrace != null)
109                 s += Environment.NewLine + StackTrace;
110
111 #if FEATURE_FUSION
112             try
113             {
114                 if(FusionLog!=null)
115                 {
116                     if (s==null)
117                         s=" ";
118                     s+=Environment.NewLine;
119                     s+=Environment.NewLine;
120                     s+=FusionLog;
121                 }
122             }
123             catch(SecurityException)
124             {
125             
126             }
127 #endif // FEATURE_FUSION
128
129             return s;
130         }
131
132         protected FileLoadException(SerializationInfo info, StreamingContext context) : base (info, context) {
133             // Base class constructor will check info != null.
134
135             _fileName = info.GetString("FileLoad_FileName");
136
137             try
138             {
139                 _fusionLog = info.GetString("FileLoad_FusionLog");
140             }
141             catch 
142             {
143                 _fusionLog = null;
144             }
145                 
146         }
147
148         private FileLoadException(String fileName, String fusionLog,int hResult)
149             : base(null)
150         {
151             SetErrorCode(hResult);
152             _fileName = fileName;
153             _fusionLog=fusionLog;
154             SetMessageField();
155         }
156
157 #if FEATURE_FUSION
158         public String FusionLog {
159             [System.Security.SecuritySafeCritical]  // auto-generated
160             [SecurityPermissionAttribute( SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.ControlPolicy)]
161             get { return _fusionLog; }
162         }
163 #endif // FEATURE_FUSION
164
165 #if FEATURE_SERIALIZATION
166         [System.Security.SecurityCritical]  // auto-generated_required
167         public override void GetObjectData(SerializationInfo info, StreamingContext context) {
168             // Serialize data for our base classes.  base will verify info != null.
169             base.GetObjectData(info, context);
170
171             // Serialize data for this class
172             info.AddValue("FileLoad_FileName", _fileName, typeof(String));
173
174             try
175             {
176                 info.AddValue("FileLoad_FusionLog", FusionLog, typeof(String));
177             }
178             catch (SecurityException)
179             {
180             }
181         }
182 #endif
183
184         [System.Security.SecuritySafeCritical]  // auto-generated
185         internal static String FormatFileLoadExceptionMessage(String fileName,
186             int hResult)
187         {
188 #if MONO
189             return string.Format (CultureInfo.InvariantCulture, "Could not load file or assembly '{0}' or one of its dependencies", fileName);
190 #else
191             string format = null;
192             GetFileLoadExceptionMessage(hResult, JitHelpers.GetStringHandleOnStack(ref format));
193
194             string message = null;
195             GetMessageForHR(hResult, JitHelpers.GetStringHandleOnStack(ref message));
196
197             return String.Format(CultureInfo.CurrentCulture, format, fileName, message);
198 #endif
199         }
200 #if !MONO
201         [System.Security.SecurityCritical]  // auto-generated
202         [ResourceExposure(ResourceScope.None)]
203         [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
204         [SuppressUnmanagedCodeSecurity]
205         private static extern void GetFileLoadExceptionMessage(int hResult, StringHandleOnStack retString);
206
207         [System.Security.SecurityCritical]  // auto-generated
208         [ResourceExposure(ResourceScope.None)]
209         [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
210         [SuppressUnmanagedCodeSecurity]
211         private static extern void GetMessageForHR(int hresult, StringHandleOnStack retString);
212 #endif
213     }
214 }