我试图做投列表为IEnumerable,所以我可以确认的是不同的列表是不为空或为空:泛型列表<T>为IEnumerable <object>
假设myList中是一个List < T>。然后在调用者的代码,我想:
Validator.VerifyNotNullOrEmpty(myList as IEnumerable<object>,
@"myList",
@"ClassName.MethodName");
的valdiating代码将是:
public static void VerifyNotNullOrEmpty(IEnumerable<object> theIEnumerable,
string theIEnumerableName,
string theVerifyingPosition)
{
string errMsg = theVerifyingPosition + " " + theIEnumerableName;
if (theIEnumerable == null)
{
errMsg += @" is null";
Debug.Assert(false);
throw new ApplicationException(errMsg);
}
else if (theIEnumerable.Count() == 0)
{
errMsg += @" is empty";
Debug.Assert(false);
throw new ApplicationException(errMsg);
}
}
然而,这好好尝试的工作。它编译,但是数字是空的!为什么?
做任何答案都有帮助吗? – 2010-12-23 23:31:49
@Dave - 哎呀。抱歉。很好的答案。接受Heinzi的,因为它更关注为什么我的代码不起作用,但你清楚地向我解释了什么应该工作。谢谢! – Avi 2011-01-04 02:00:39