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.
4 
5 /*=============================================================================
6 **
7 **
8 **
9 ** Purpose: The exception class to wrap exceptions thrown by
10 **          a type's class initializer (.cctor).  This is sufficiently
11 **          distinct from a TypeLoadException, which means we couldn't
12 **          find the type.
13 **
14 **
15 =============================================================================*/
16 
17 using System.Globalization;
18 using System.Runtime.Serialization;
19 
20 namespace System
21 {
22     [Serializable]
23     [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
24     public sealed class TypeInitializationException : SystemException
25     {
26         private String _typeName;
27 
28         // This exception is not creatable without specifying the
29         //    inner exception.
TypeInitializationException()30         private TypeInitializationException()
31             : base(SR.TypeInitialization_Default)
32         {
33             HResult = HResults.COR_E_TYPEINITIALIZATION;
34         }
35 
36 
TypeInitializationException(String fullTypeName, Exception innerException)37         public TypeInitializationException(String fullTypeName, Exception innerException)
38             : this(fullTypeName, SR.Format(SR.TypeInitialization_Type, fullTypeName), innerException)
39         {
40         }
41 
42         // This is called from within the runtime.  I believe this is necessary
43         // for Interop only, though it's not particularly useful.
TypeInitializationException(String message)44         internal TypeInitializationException(String message) : base(message)
45         {
46             HResult = HResults.COR_E_TYPEINITIALIZATION;
47         }
48 
TypeInitializationException(String fullTypeName, String message, Exception innerException)49         internal TypeInitializationException(String fullTypeName, String message, Exception innerException)
50             : base(message, innerException)
51         {
52             _typeName = fullTypeName;
53             HResult = HResults.COR_E_TYPEINITIALIZATION;
54         }
55 
TypeInitializationException(SerializationInfo info, StreamingContext context)56         internal TypeInitializationException(SerializationInfo info, StreamingContext context)
57             : base(info, context)
58         {
59             _typeName = info.GetString("TypeName");
60         }
61 
GetObjectData(SerializationInfo info, StreamingContext context)62         public override void GetObjectData(SerializationInfo info, StreamingContext context)
63         {
64             base.GetObjectData(info, context);
65             info.AddValue("TypeName", TypeName, typeof(string));
66         }
67 
68         public String TypeName
69         {
70             get
71             {
72                 if (_typeName == null)
73                 {
74                     return String.Empty;
75                 }
76                 return _typeName;
77             }
78         }
79     }
80 }
81