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.
5 /*=============================================================================
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
15 =============================================================================*/
17 using System
.Globalization
;
18 using System
.Runtime
.Serialization
;
23 [System
.Runtime
.CompilerServices
.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
24 public sealed class TypeInitializationException
: SystemException
26 private readonly string? _typeName
;
28 // This exception is not creatable without specifying the
30 private TypeInitializationException()
31 : base(SR
.TypeInitialization_Default
)
33 HResult
= HResults
.COR_E_TYPEINITIALIZATION
;
37 public TypeInitializationException(string? fullTypeName
, Exception
? innerException
)
38 : this(fullTypeName
, SR
.Format(SR
.TypeInitialization_Type
, fullTypeName
), innerException
)
42 // This is called from within the runtime. I believe this is necessary
43 // for Interop only, though it's not particularly useful.
44 internal TypeInitializationException(string? message
) : base(message
)
46 HResult
= HResults
.COR_E_TYPEINITIALIZATION
;
49 internal TypeInitializationException(string? fullTypeName
, string? message
, Exception
? innerException
)
50 : base(message
, innerException
)
52 _typeName
= fullTypeName
;
53 HResult
= HResults
.COR_E_TYPEINITIALIZATION
;
56 private TypeInitializationException(SerializationInfo info
, StreamingContext context
)
59 _typeName
= info
.GetString("TypeName");
62 public override void GetObjectData(SerializationInfo info
, StreamingContext context
)
64 base.GetObjectData(info
, context
);
65 info
.AddValue("TypeName", TypeName
, typeof(string));
68 public string TypeName
=> _typeName
?? string.Empty
;