In my WPF application, I have the following line:
var windowList = Application.Current.Windows.OfType<Window>().ToList();
This is returning 2 results. These results have the following types:
Microsoft.Crm.UnifiedServiceDesk.Desktop
System.Windows.Window
I only want the 2nd result to be returned. However, since result #1 inherits System.Windows.Window
, it is included in the result.
Is there a way to restrict the OfType<T>()
function to NOT return inherited classes, or do I have to accomplish this another way?
You can't use OfType
on its own to achieve this, but you can use that with a Where
clause afterwards:
var windowList = Application.Current.Windows
.OfType<Window>()
.Where(w => w.GetType() == typeof(Window))
.ToList();
See more on this question at Stackoverflow