Is there a way to get an IEnumerable<T> from an IEnumerable without reflection, assuming I know the type at design time?
I have this
foreach(DirectoryEntry child in de.Children)
{
// long running code on each child object
}
I am trying to enable parallelization, like so
Parallel.ForEach(de.Children,
(DirectoryEntry child) => { // long running code on each child });
but this doesn’t work, as de.Children is of type DirectoryEntries. It implements IEnumerable but not IEnumerable<DirectoryEntry>.
The way to achieve this is to use the
.Cast<T>()extension method.Another way to achieve this is to use the
.OfType<T>()extension method.There is a subtle different between
.Cast<T>()and.OfType<T>()— MSDN
This link on the MSDN forums got me going the right direction.