T 在运行时是已知的。但是我想在编译时将 T 转换为IEnumerable对象类型并循环遍历。
public void x<T>(T obj)
{
//T is IEnumerable<object>
//how to loop through T on compile time?
}
答案 0 :(得分:1)
这听起来很简单
public void x<T>(T obj)
{
//T is IEnumerable<object>
//how to loop through T on compile time?
try
{
foreach (var item in (IEnumerable)obj)
{
// Do what you want
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}