summaryrefslogtreecommitdiff
path: root/src/System.Private.CoreLib/shared/System/TypeLoadException.cs
blob: 92afe347e24fa2a2a6ba8d6b9e1c52bc4aca7c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Runtime.Serialization;

namespace System
{
    [Serializable]
    [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
    public partial class TypeLoadException : SystemException, ISerializable
    {
        public TypeLoadException()
            : base(SR.Arg_TypeLoadException)
        {
            HResult = HResults.COR_E_TYPELOAD;
        }

        public TypeLoadException(string? message)
            : base(message)
        {
            HResult = HResults.COR_E_TYPELOAD;
        }

        public TypeLoadException(string? message, Exception? inner)
            : base(message, inner)
        {
            HResult = HResults.COR_E_TYPELOAD;
        }

        public override string Message
        {
            get
            {
                SetMessageField();
                return _message!;
            }
        }

        public string TypeName => _className ?? string.Empty;

        protected TypeLoadException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
            _className = info.GetString("TypeLoadClassName");
            _assemblyName = info.GetString("TypeLoadAssemblyName");
            _messageArg = info.GetString("TypeLoadMessageArg");
            _resourceId = info.GetInt32("TypeLoadResourceID");
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("TypeLoadClassName", _className, typeof(string));
            info.AddValue("TypeLoadAssemblyName", _assemblyName, typeof(string));
            info.AddValue("TypeLoadMessageArg", _messageArg, typeof(string));
            info.AddValue("TypeLoadResourceID", _resourceId);
        }

        // If ClassName != null, GetMessage will construct on the fly using it
        // and ResourceId (mscorrc.dll). This allows customization of the
        // class name format depending on the language environment.
        private string? _className;
        private string? _assemblyName;
        private readonly string? _messageArg;
        private readonly int _resourceId;
    }
}