diff --git a/Falcon.SugarApi.Test/ObjectExtendTest.cs b/Falcon.SugarApi.Test/ObjectExtendTest.cs index 542ed78..80fcbef 100644 --- a/Falcon.SugarApi.Test/ObjectExtendTest.cs +++ b/Falcon.SugarApi.Test/ObjectExtendTest.cs @@ -87,6 +87,28 @@ namespace Falcon.SugarApi.Test } } } + + /// + /// 是否为空测试 + /// + [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()); + } } /// /// 扩展属性测试类 diff --git a/Falcon.SugarApi/ObjectExtend.cs b/Falcon.SugarApi/ObjectExtend.cs index 95ddbca..eadb356 100644 --- a/Falcon.SugarApi/ObjectExtend.cs +++ b/Falcon.SugarApi/ObjectExtend.cs @@ -90,6 +90,20 @@ namespace Falcon.SugarApi }; } } + + /// + /// 对象是否为null + /// + /// 要测试的对象 + /// True表示对象为null,否则不为null + public static bool IsNull([AllowNull] this object obj) => obj==null; + + /// + /// 对象是否不为null。与IsNull相反 + /// + /// 要测试的对象 + /// True表示对象不为null,否则为null + public static bool IsNotNull([AllowNull] this object obj) => !obj.IsNull(); } ///