diff --git a/Falcon.SugarApi.Test/ObjectExtendTest.cs b/Falcon.SugarApi.Test/ObjectExtendTest.cs index 76d8a5c..8194296 100644 --- a/Falcon.SugarApi.Test/ObjectExtendTest.cs +++ b/Falcon.SugarApi.Test/ObjectExtendTest.cs @@ -125,6 +125,26 @@ namespace Falcon.SugarApi.Test Assert.IsTrue(fullCount > nFullCount); } + + /// + /// 测试In方法 + /// + [TestMethod] + public void ObjectInTest() { + string obj = "abc"; + Assert.IsTrue(obj.In("eee","abc","1231")); + Assert.IsFalse(obj.In("2313","eeee")); + + var o1 = new object(); + Assert.IsTrue(o1.In(o1)); + Assert.IsFalse(o1.In( new object(),new object())); + + var o2 = new ObjWithComparable { val = 1 }; + var o22 = new ObjWithComparable { val = 1 }; + var o23 = new ObjWithComparable { val = 1 }; + Assert.IsTrue(o2.In(o22,o23)); + } + } /// /// 扩展属性测试类 @@ -157,4 +177,18 @@ namespace Falcon.SugarApi.Test { public string ItemA { get; set; } = "itema"; } + + public class ObjWithComparable + { + public int val { get; set; } + + public int CompareTo(ObjWithComparable? other) { + return this.val - other.val; + } + + public override bool Equals(object? obj) { + var c = obj as ObjWithComparable; + return c == null ? false : c.val == this.val; + } + } } diff --git a/Falcon.SugarApi.sln b/Falcon.SugarApi.sln index d482c54..7f65dd1 100644 --- a/Falcon.SugarApi.sln +++ b/Falcon.SugarApi.sln @@ -21,7 +21,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RabbitMqTest", "RabbitMqTes EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpTest", "CSharpTest\CSharpTest.csproj", "{FA6A7A47-7EEE-4264-9C89-C2E717B1CB78}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NugetPluginTest", "NugetPluginTest\NugetPluginTest.csproj", "{3570FDA6-C408-4A61-852E-3B4F1DCAD1AD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NugetPluginTest", "NugetPluginTest\NugetPluginTest.csproj", "{3570FDA6-C408-4A61-852E-3B4F1DCAD1AD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Falcon.SugarApi/ObjectExtend.cs b/Falcon.SugarApi/ObjectExtend.cs index fec413f..59a35cd 100644 --- a/Falcon.SugarApi/ObjectExtend.cs +++ b/Falcon.SugarApi/ObjectExtend.cs @@ -208,6 +208,26 @@ namespace Falcon.SugarApi targetBuilder?.Invoke(obj); return obj as T; } + + /// + /// 判断对象是否在目标对象数组中。如果对象实现IComparable,则使用IComparable比较对象,否则使用Equals方法比较对象 + /// + /// 对象类型 + /// 当前对象 + /// 目标对象数组 + /// 在数组中返回True,否则False + public static bool In(this T obj,params T[] values) { + if(obj == null) { + return values.Any(a => a == null); + } + foreach(var i in values) { + if(obj.Equals(i)) { + return true; + } + } + return false; + } + } ///