[英]How to tell “OfType()” to ignore inherited classes?
In my WPF application, I have the following line: 在我的WPF应用程序中,我有以下内容:
var windowList = Application.Current.Windows.OfType<Window>().ToList();
This is returning 2 results. 这将返回2个结果。 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. 但是,由于结果#1继承了System.Windows.Window
,因此它包含在结果中。
Is there a way to restrict the OfType<T>()
function to NOT return inherited classes, or do I have to accomplish this another way? 有没有一种方法可以限制OfType<T>()
函数不返回继承的类,还是我必须以另一种方式来完成此工作?
You can't use OfType
on its own to achieve this, but you can use that with a Where
clause afterwards: 您不能OfType
使用OfType
来实现此目的,但是之后可以将其与Where
子句一起使用:
var windowList = Application.Current.Windows
.OfType<Window>()
.Where(w => w.GetType() == typeof(Window))
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.