在C#中使用OfType过滤类型的对象
我有一个基类Base,以及inheritance自它的A / B类。
public class Base { int x; } public class A : Base { int y; } public class B : Base { int z; }
我尝试使用OfType来过滤我需要的唯一对象,如下所示:
public static void RunSnippet() { Base xbase; A a; B b; IEnumerable list = new List() {xbase, a, b}; Base f = list.OfType; // I need to get only the object A Console.WriteLine(f); }
当我编译代码时,我收到此错误:
错误CS0428:无法将方法组’OfType’转换为非委托类型’Base’。 你打算调用这个方法吗?
代码有什么问题?