I have a method similar to this:
void Log(object value)
{
if (value is IEnumerable && !(value is IList))
{
// Need to convert value to a list here
}
// ... Rest of code here
}
As the comment says, I need to check if the value
passed in is actually an IEnumerable
and convert it to a List
if it is.
The problem is that the IEnumerable
's generic type could be anything. Application classes, .Net Library classes, even anonymous projections via a linq select statement.
How can I convert an IEnumerable
(without knowing its generic type) to a List
?
IMPORTANT: This is using the full .Net Framework 4.x. NOT .Net Core.
FYI: This is in legacy code. The reason that I need to do this is because the library we use to serialize our logs (YAX) has a memory leak with IEnumerable
s. But we have found that if we convert to a List
then the memory leak does not happen.