C# 开发中判断object对象是否为List集合且存在元素
public static bool IsListWithElements(object obj)
{
// Check if the object is null.
if (obj == null)
return false;
// Check if the object is a list.
if (obj is IList && obj.GetType().IsGenericType)
{
Type type = obj.GetType().GetGenericTypeDefinition();
if (type == typeof(List<>))
{
// The object is a List<T>, now check if it has elements.
return ((IList)obj).Count > 0;
}
}
// The object is not a List<T> or doesn't have any elements.
return false;
} 


评论一下吧
取消回复 评论守则