新增对象是否为null的检测方法IsNull和IsNotNull

This commit is contained in:
falcon 2023-02-06 15:43:21 +08:00
parent 8cf7880575
commit 02c2c8843e
2 changed files with 36 additions and 0 deletions

View File

@ -87,6 +87,28 @@ namespace Falcon.SugarApi.Test
}
}
}
/// <summary>
/// 是否为空测试
/// </summary>
[TestMethod]
public void IsNullTest() {
object obj = null;
Assert.IsTrue(obj.IsNull());
Assert.IsFalse(obj.IsNotNull());
obj=new object();
Assert.IsTrue(obj.IsNotNull());
Assert.IsFalse(obj.IsNull());
object? obj1 = null;
Assert.IsTrue(obj1.IsNull());
Assert.IsFalse(obj1.IsNotNull());
obj1=new object();
Assert.IsTrue(obj1.IsNotNull());
Assert.IsFalse(obj1.IsNull());
}
}
/// <summary>
/// 扩展属性测试类

View File

@ -90,6 +90,20 @@ namespace Falcon.SugarApi
};
}
}
/// <summary>
/// 对象是否为null
/// </summary>
/// <param name="obj">要测试的对象</param>
/// <returns>True表示对象为null否则不为null</returns>
public static bool IsNull([AllowNull] this object obj) => obj==null;
/// <summary>
/// 对象是否不为null。与IsNull相反
/// </summary>
/// <param name="obj">要测试的对象</param>
/// <returns>True表示对象不为null否则为null</returns>
public static bool IsNotNull([AllowNull] this object obj) => !obj.IsNull();
}
/// <summary>