Lista genérica <T> como IEnumerable <object>
Estou tentando converter uma lista em um IEnumerable, para que eu possa verificar se listas diferentes não são nulas ou vazias:
Suponha que myList seja uma lista <T>. Então, no código de chamada, eu queria:
Validator.VerifyNotNullOrEmpty(myList as IEnumerable<object>,
@"myList",
@"ClassName.MethodName");
O código de validação seria:
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);
}
}
No entanto, isso não funciona. Compila, mas theIEnumerable é nulo! Por quê?