Skip to content

Commit f99526a

Browse files
committed
fix(extensions): 为 IsNull/IsNotNull 添加 NotNullWhen 特性
添加 NotNullWhen 特性使编译器静态分析能够正确识别空值检查, 解决 IDE 在使用 IsNotNull 判断后仍提示可能为空的警告问题。
1 parent fc6089c commit f99526a

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

GameFrameX.Foundation.Extensions/ObjectExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
66

7+
using System.Diagnostics.CodeAnalysis;
78
using GameFrameX.Foundation.Localization.Core;
89
using GameFrameX.Foundation.Extensions.Localization;
910

@@ -24,7 +25,7 @@ public static class ObjectExtensions
2425
/// 此方法仅适用于引用类型,对值类型调用此方法会导致编译错误。
2526
/// 这是为了避免对值类型(永远不会为null)调用此方法产生误导性结果。
2627
/// </remarks>
27-
public static bool IsNull<T>(this T self) where T : class
28+
public static bool IsNull<T>([NotNullWhen(false)] this T self) where T : class
2829
{
2930
return self is null;
3031
}
@@ -39,7 +40,7 @@ public static bool IsNull<T>(this T self) where T : class
3940
/// 此方法仅适用于引用类型,对值类型调用此方法会导致编译错误。
4041
/// 这是为了避免对值类型(永远不会为null)调用此方法产生误导性结果。
4142
/// </remarks>
42-
public static bool IsNotNull<T>(this T self) where T : class
43+
public static bool IsNotNull<T>([NotNullWhen(true)] this T self) where T : class
4344
{
4445
return self is not null;
4546
}

0 commit comments

Comments
 (0)