我有此代码以获得“A”作为过滤结果。OfType <????>当在C#中使用方法的参数
public static void RunSnippet()
{
Base xbase = new Base();
A a = new A();
B b = new B();
IEnumerable<Base> list = new List<Base>() { xbase, a, b };
Base f = list.OfType<A>().FirstOrDefault();
Console.WriteLine(f);
}
我需要使用IEnumerable<Base> list = new List<Base>() {xbase, a, b};
从功能如下:
public static Base Method(IEnumerable<Base> list, Base b (????)) // I'm not sure I need Base b parameter for this?
{
Base f = list.OfType<????>().FirstOrDefault();
return f;
}
public static void RunSnippet()
{
Base xbase = new Base();
A a = new A();
B b = new B();
IEnumerable<Base> list = new List<Base>() { xbase, a, b };
//Base f = list.OfType<A>().FirstOrDefault();
Base f = Method(list);
Console.WriteLine(f);
}
我在使用什么参数 '????'从原始代码中获得相同的结果?
你不能调用'Method(list)' - 'list'不是'Base',它是'IEnumerable '。这是你第二次犯这个错误 - 你用IEnumerable ''有多舒服?并且'Method' *总是*意味着返回一个'A'值?如果是这样,为什么它会声明返回'Base',为什么不能使用'A'而不是'''? –
2012-04-15 20:31:18
@Jon:Method()的参数应该是'IEnumerable list,Base b'。对于????,我需要从第二个参数中获得类型A.我尝试使用(Base b)作为参数,并在???>中使用b.GetType(),但它不起作用,因为b.GetType()返回Type not base。 –
prosseek
2012-04-15 20:40:54