From 02c2c8843e2ebd650a84832fc19bad388d0a073a Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Mon, 6 Feb 2023 15:43:21 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AF=B9=E8=B1=A1=E6=98=AF?=
=?UTF-8?q?=E5=90=A6=E4=B8=BAnull=E7=9A=84=E6=A3=80=E6=B5=8B=E6=96=B9?=
=?UTF-8?q?=E6=B3=95IsNull=E5=92=8CIsNotNull?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Falcon.SugarApi.Test/ObjectExtendTest.cs | 22 ++++++++++++++++++++++
Falcon.SugarApi/ObjectExtend.cs | 14 ++++++++++++++
2 files changed, 36 insertions(+)
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();
}
///