Generic List <T> als IEnumerable <object>
Ich versuche, eine Liste in eine IEnumerable umzuwandeln, damit ich überprüfen kann, ob verschiedene Listen nicht null oder leer sind:
Angenommen, myList ist eine Liste <T>. Dann im Anrufercode wollte ich:
Validator.VerifyNotNullOrEmpty(myList as IEnumerable<object>,
@"myList",
@"ClassName.MethodName");
Der Bestätigungscode wäre:
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);
}
}
Allerdings funktioniert das nicht. Es kompiliert, aber theIEnumerable ist null! Warum