我想有一個擴展方法來打印X>如何將泛型參數轉換爲泛型接口?
public static IEnumerable<T> log_elements<T>(this IEnumerable<T> collection, bool recursive = true)
{
Form1.log("[");
foreach (T i in collection)
if(recursive&&(i.GetType().GetGenericTypeDefinition()==typeof(IEnumerable<>)))
(IEnumerable<>)i.log_elements();//This doesn't work
else
Form1.log(i);
Form1.log("]");
return collection;
}
的IEnumerable <的所有內容如果它包含了IEnumerable < Y>,該方法應該要求它。
我不能添加log_elements<T>(this IEnumerable<IEnumerable<T>>,bool)
,因爲IEnumerable<T>
匹配原始方法的T
。
我幾乎可以肯定,應該有一個在c#中這樣簡單的問題的解決方案。
看來您實際上並不需要「IEnumerable」的通用形式。 –