Skip to content

Commit c3cd51a

Browse files
authoredMar 1, 2025··
refactor: Nullable and Modernization for Core Presentation projects (#2654)
* [Core.Presentation] Modernize code * [Core.Design] Make ToEnumerable extension safer It will not throw a cast exception for a type mismatch, but instead filter out bad items. * [Core.Quantum] Modernize code * [Core.Quantum.Tests] Modernize code * [Core.Presentation.Quantum] Modernize code * [Core.Presentation.Quantum.Tests] Modernize code
1 parent 0d0c656 commit c3cd51a

File tree

118 files changed

+9309
-9591
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+9309
-9591
lines changed
 

‎sources/core/Stride.Core.Design/Extensions/ObjectExtensions.cs

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Collections;
55
using System.Runtime.CompilerServices;
6-
using Stride.Core.Annotations;
76

87
namespace Stride.Core.Extensions;
98

@@ -17,8 +16,7 @@ public static class ObjectExtensions
1716
/// </summary>
1817
/// <param name="obj">The object.</param>
1918
/// <returns>The return value of <see cref="object.ToString"/>, or "(null)" if <see ref="obj"/> is null, or (ExceptionInToString)" if <see cref="object.ToString"/> thrown an exception.</returns>
20-
[NotNull]
21-
public static string ToStringSafe([CanBeNull] this object obj)
19+
public static string ToStringSafe(this object? obj)
2220
{
2321
try
2422
{
@@ -54,8 +52,16 @@ public static string ToStringSafe([CanBeNull] this object obj)
5452
if (obj is IEnumerable<T?> enumerableT)
5553
return enumerableT;
5654

57-
var enumerable = obj as IEnumerable;
58-
return enumerable?.Cast<T>() ?? Yield((T?)obj);
55+
if (obj is IEnumerable enumerable)
56+
return enumerable.OfType<T>();
57+
58+
if (obj is null)
59+
return Yield((T?)obj);
60+
61+
if (obj is T t)
62+
return Yield(t);
63+
64+
return [];
5965
}
6066

6167
/// <summary>
@@ -67,7 +73,6 @@ public static string ToStringSafe([CanBeNull] this object obj)
6773
/// <param name="argumentName">The name of the argument, in case an <see cref="ArgumentNullException"/> must be thrown.</param>
6874
/// <returns>The given object.</returns>
6975
/// <remarks>This method can be used to test for null argument when forwarding members of the object to the <c>base</c> or <c>this</c> constructor.</remarks>
70-
[NotNull]
7176
public static T SafeArgument<T>(this T obj, [CallerArgumentExpression(nameof(obj))] string argumentName = "") where T : class
7277
{
7378
#if NET6_0_OR_GREATER

‎sources/core/Stride.Core.Reflection/ITypeDescriptorFactory.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Stride.Core.Reflection;
57

68
/// <summary>
@@ -19,5 +21,6 @@ public interface ITypeDescriptorFactory
1921
/// </summary>
2022
/// <param name="type">The type.</param>
2123
/// <returns>ITypeDescriptor.</returns>
24+
[return: NotNullIfNotNull(nameof(type))]
2225
ITypeDescriptor? Find(Type? type);
2326
}

0 commit comments

Comments
 (0)
Please sign in to comment.