如果T是一个数组,我希望将其转换为T[]。
static T GenericFunction<T>(T t)
{
if (t == null) return default(T);
if (t.GetType().IsArray)
{
//if object is an array it should be handled
//by an array method
return (T) GenericArrayFunction((T[])t);
}
...
}
static T[] GenericArrayFunction<T>(T[] t)
{
if (t == null) return default(T);
for (int i = 0 ; i < t.Length ; i++)
{
//for each element in array carry
//out Generic Function
if (t[i].GetType().IsArray())
{
newList[i] = GenericArrayFunction((T[])t[i]);
}
else
{
newList[i] = GenericFunction(t[i]);
}
}
...
}
错误 如果我尝试使用(T[])t
无法将类型'T'转换为'T[]'
错误 如果我只是尝试传递t
方法'GenericArrayFunction(T[])'的类型参数无法从使用中推断出来。请尝试明确指定类型参数。
T
是一个数组,那么T[]
就是一个二维数组。这可能不是你所期望的。 - GabeT
是一个数组,那么T[]
就是一个数组的数组。我怀疑这不是你想要的。你是不是想说,如果T
是一个未知元素类型U
的数组,那么你想要的类型是"U的数组"U[]
? - jalf