You left out some requirement detail, but I think something like this should work:
public IEnumerable<BaseClass> AllIsSelected(BaseClass root)
{
if (root.IsSelected)
{
yield return root;
}
var composite = root as DerivedOne;
if (composite != null)
{
foreach (var v in composite.Children)
{
foreach (var x in AllIsSelected(v))
{
yield return x;
}
}
}
}
Of course, if you want a full list all at once, you could build the list instead of using 'yield'.
This is the same design discussed here: IEnumerable and Recursion using yield return.
As another answer said, you can use LINQ to shorten this somewhat. This version avoids making the temporary list.
public IEnumerable<BaseClass> AllIsSelected(BaseClass root)
{
if (root.IsSelected)
{
yield return root;
}
var composite = root as DerivedOne;
if (composite != null)
{
foreach (var x in composite.Children.SelectMany(v => AllIsSelected(v)))
{
yield return x;
}
}
}