Skip to content

Commit d65dc6a

Browse files
committed
fix(extensions): 修复 ObjectExtensions.IsNull/IsNotNull 泛型约束问题
- 添加 where T : class 约束,限制为引用类型 - 避免对值类型调用产生误导性结果(值类型永远不会为 null)
1 parent 48f1308 commit d65dc6a

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

GameFrameX.Foundation.Extensions/ObjectExtensions.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,31 @@ namespace GameFrameX.Foundation.Extensions;
1515
public static class ObjectExtensions
1616
{
1717
/// <summary>
18-
/// 检查对象是否为null
18+
/// 检查引用类型对象是否为null
1919
/// </summary>
20+
/// <typeparam name="T">对象的类型,必须为引用类型</typeparam>
2021
/// <param name="self">要检查的对象</param>
2122
/// <returns>如果对象为null,则返回true;否则返回false</returns>
22-
public static bool IsNull(this object self)
23+
/// <remarks>
24+
/// 此方法仅适用于引用类型,对值类型调用此方法会导致编译错误。
25+
/// 这是为了避免对值类型(永远不会为null)调用此方法产生误导性结果。
26+
/// </remarks>
27+
public static bool IsNull<T>(this T self) where T : class
2328
{
2429
return self is null;
2530
}
2631

2732
/// <summary>
28-
/// 检查对象是否不为null
33+
/// 检查引用类型对象是否不为null
2934
/// </summary>
35+
/// <typeparam name="T">对象的类型,必须为引用类型</typeparam>
3036
/// <param name="self">要检查的对象</param>
3137
/// <returns>如果对象不为null,则返回true;否则返回false</returns>
32-
public static bool IsNotNull(this object self)
38+
/// <remarks>
39+
/// 此方法仅适用于引用类型,对值类型调用此方法会导致编译错误。
40+
/// 这是为了避免对值类型(永远不会为null)调用此方法产生误导性结果。
41+
/// </remarks>
42+
public static bool IsNotNull<T>(this T self) where T : class
3343
{
3444
return self is not null;
3545
}

0 commit comments

Comments
 (0)