![](/img/trans.png)
[英]C# is there any better way to check if more than 2 properties in an object is not null
[英]Check string null in any better way C#
一個方法接受大約7個字符串參數,並且它們需要被檢查為null所有這些參數的基本方法是簡單的!string.IsNullOrWhitespace(param1),同樣有更好或更聰明的方法嗎? 另外,如果有幫助,我可以將參數包裝在一個對象中!
如果任何 null
對您來說都是假的:
bool CheckAnyNull(params string[] strs)
{
foreach (var str in strs)
{
if (strs.IsNullOrWhiteSpace(str))
return true;
}
return false;
}
如果要檢查它們是否全為 null
:
bool CheckAllNull(params string[] strs)
{
foreach (var str in strs)
{
if (!strs.IsNullOrWhiteSpace(str))
return false;
}
return true;
}
如果您想要特定數量的null
字符串:
int CheckNullCount(params string[] strs)
{
int count;
foreach (var str in strs)
{
if (strs.IsNullOrWhiteSpace(str))
count++;
}
return count;
}
然后可以像這樣調用所有這些:
CheckAnyNulls(str1, str2, str3, str4, str5, str6, str7);
CheckAllNulls(str1, str2, str3, str4, str5, str6, str7);
int nullStrs = CheckNullCount(str1, str2, str3, str4, str5, str6, str7);
要么:
var strs = new string[7];
// populate the strs array.
CheckAnyNulls(strs);
CheckAllNulls(strs);
int nullStrs = CheckNullCount(strs);
如果你想要返回任何一個字符串不是空的,那么這是一個很好的解決方案:
public static bool AreAllStringsEmpty(params string[] array)
{
return array.All(str => String.IsNullOrWhiteSpace(str));
}
public static bool AreAllStringsFull(params string[] array)
{
return array.All(str => !String.IsNullOrWhiteSpace(str));
}
public static bool AreAtleastOneStringFull(params string[] array)
{
return array.Any(str => !String.IsNullOrWhiteSpace(str));
}
而不是像這樣使用它:
AreAllStringsFull(param1, param2, ...);
備注:您可以添加擴展方法來簡化流程,但它仍然是樣板代碼。
您可以將字符串傳遞給列表,然后像這樣檢查:
if(List.All(x=>string.IsNullOrWhiteSpace(x)))
{
}
如果您不需要檢查空格,只需比較null:
if (param1 != null)
通常,如果輸入必須不為null,則會拋出ArgumentNullException :
if (param1 == null)
throw new ArgumentNullException("param1");
您可以使用反射構建擴展方法,以立即檢查整個對象。 但是,由於這會變得很昂貴,因此可以使用FastMember等庫來最大限度地降低后續對象檢查的反射成本。
using System.Linq;
using FastMember;
namespace Utilities
{
public static class CheckStringProperties
{
public static bool AreAnyStringPropertiesNull<T>(this T model) where T : class
{
var accessor = TypeAccessor.Create(model.GetType());
return AreAnyStringPropertiesNull(model, accessor);
}
public static bool AreAnyStringPropertiesNull<T>(this T model) where T : class
{
var accessor = TypeAccessor.Create(model.GetType());
return AreAllStringPropertiesNull(model, accessor);
}
private static bool AreAnyStringPropertiesNull<T>(T model, TypeAccessor accessor)
{
foreach (var strng in GetStringProperties(accessor))
{
if (string.IsNullOrWhiteSpace(strng))
return true;
}
return false;
}
private static bool AreAllStringPropertiesNull<T>(T model, TypeAccessor accessor)
{
foreach (var strng in GetStringProperties(accessor))
{
if (!string.IsNullOrWhiteSpace(strng))
return true;
}
return false;
}
private static List<Member> GetStringProperties(TypeAccessor accessor)
{
return accessor.GetMembers().Where(x => x.Type == typeof(string)).ToList();
}
}
}
然后,您可以在類上調用擴展方法。
var containsNulls = classInstance.AreAnyStringPropertiesNull();
var areAllNull = classInstance.AreAllStringPropertiesNull();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.