2012-04-15 57 views
2

我有一个基类Base和类A/B从它继承。在C中用OfType过滤一个类型的对象#

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<Base> list = new List<Base>() {xbase, a, b}; 
    Base f = list.OfType<A>; // I need to get only the object A 
    Console.WriteLine(f); 
} 

当我编译的代码,我得到这个错误:

error CS0428: Cannot convert method group 'OfType' to non-delegate type 'Base'. Did you intend to invoke the method?

什么问题用代码?

回答