我有此代碼以獲得「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