diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/ReflectionTypeLoadException.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/ReflectionTypeLoadException.cs index 29a624e6cf5196..f108d883b2e766 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/ReflectionTypeLoadException.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/ReflectionTypeLoadException.cs @@ -2,35 +2,34 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Runtime.CompilerServices; using System.Runtime.Serialization; using System.Text; namespace System.Reflection { [Serializable] - [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] + [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public sealed class ReflectionTypeLoadException : SystemException, ISerializable { - public ReflectionTypeLoadException(Type[]? classes, Exception?[]? exceptions) - : base(null) + public ReflectionTypeLoadException(Type?[]? classes, Exception?[]? exceptions) : + this(classes, exceptions, null) { - Types = classes; - LoaderExceptions = exceptions; - HResult = HResults.COR_E_REFLECTIONTYPELOAD; } - public ReflectionTypeLoadException(Type[]? classes, Exception?[]? exceptions, string? message) + public ReflectionTypeLoadException(Type?[]? classes, Exception?[]? exceptions, string? message) : base(message) { - Types = classes; - LoaderExceptions = exceptions; + Types = classes ?? Type.EmptyTypes; + LoaderExceptions = exceptions ?? Array.Empty(); HResult = HResults.COR_E_REFLECTIONTYPELOAD; } private ReflectionTypeLoadException(SerializationInfo info, StreamingContext context) : base(info, context) { - LoaderExceptions = (Exception[]?)(info.GetValue("Exceptions", typeof(Exception[]))); + Types = Type.EmptyTypes; + LoaderExceptions = (Exception?[]?)info.GetValue("Exceptions", typeof(Exception[])) ?? Array.Empty(); } public override void GetObjectData(SerializationInfo info, StreamingContext context) @@ -40,9 +39,9 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont info.AddValue("Exceptions", LoaderExceptions, typeof(Exception[])); } - public Type[]? Types { get; } + public Type?[] Types { get; } - public Exception?[]? LoaderExceptions { get; } + public Exception?[] LoaderExceptions { get; } public override string Message => CreateString(isMessage: true); @@ -52,8 +51,8 @@ private string CreateString(bool isMessage) { string baseValue = isMessage ? base.Message : base.ToString(); - Exception?[]? exceptions = LoaderExceptions; - if (exceptions == null || exceptions.Length == 0) + Exception?[] exceptions = LoaderExceptions; + if (exceptions.Length == 0) { return baseValue; } @@ -63,10 +62,10 @@ private string CreateString(bool isMessage) { if (e != null) { - text.AppendLine(); - text.Append(isMessage ? e.Message : e.ToString()); + text.AppendLine().Append(isMessage ? e.Message : e.ToString()); } } + return text.ToString(); } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index d203dcc3d17be3..b04189e50904cc 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -6306,11 +6306,11 @@ protected ReflectionContext() { } } public sealed partial class ReflectionTypeLoadException : System.SystemException, System.Runtime.Serialization.ISerializable { - public ReflectionTypeLoadException(System.Type[]? classes, System.Exception?[]? exceptions) { } - public ReflectionTypeLoadException(System.Type[]? classes, System.Exception?[]? exceptions, string? message) { } - public System.Exception?[]? LoaderExceptions { get { throw null; } } + public ReflectionTypeLoadException(System.Type?[]? classes, System.Exception?[]? exceptions) { } + public ReflectionTypeLoadException(System.Type?[]? classes, System.Exception?[]? exceptions, string? message) { } + public System.Exception?[] LoaderExceptions { get { throw null; } } public override string Message { get { throw null; } } - public System.Type[]? Types { get { throw null; } } + public System.Type?[] Types { get { throw null; } } public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public override string ToString() { throw null; } } diff --git a/src/libraries/System.Runtime/tests/System/Reflection/ReflectionTypeLoadExceptionTests.cs b/src/libraries/System.Runtime/tests/System/Reflection/ReflectionTypeLoadExceptionTests.cs index a1ee4e6003dadd..bd6bc131508acd 100644 --- a/src/libraries/System.Runtime/tests/System/Reflection/ReflectionTypeLoadExceptionTests.cs +++ b/src/libraries/System.Runtime/tests/System/Reflection/ReflectionTypeLoadExceptionTests.cs @@ -28,8 +28,8 @@ public static void NullArgumentsNoNullPointerException() ReflectionTypeLoadException rtle = new ReflectionTypeLoadException(null, null, "Null arguments"); Assert.NotNull(rtle.ToString()); Assert.NotNull(rtle.Message); - Assert.Null(rtle.LoaderExceptions); - Assert.Null(rtle.Types); + Assert.Empty(rtle.LoaderExceptions); + Assert.Empty(rtle.Types); } } }