I have the following code:
public static List<object[]> Serialise2D_Rec<T>(IEnumerable<T> data)
{
int numElts = 0;
foreach (var item in data)
numElts++;
Type t = typeof(T); // Get type pointer
PropertyInfo[] propList = t.GetProperties();
List<object[]> toret = new List<object[]>();
for (long propID = 0; propID < propList.Count(); ++propID)
{
var proptype = propList[propID].PropertyType;
if (proptype.IsPrimitive || proptype == typeof(Decimal) || proptype == typeof(String))
{
toret.Add(new object[numElts + 1]);
toret[toret.Count - 1][0] = propList[propID].Name;
int row = 1;
foreach (T item in data)
{
toret[toret.Count - 1][row] = propList[propID].GetValue(item, null);
row++;
}
}
else
{
var lst = (IList)Activator.CreateInstance((typeof(List<>).MakeGenericType(proptype)));
foreach (T item in data)
{
lst.Add(propList[propID].GetValue(item, null));
}
List<object[]> serialisedProp = Serialise2D_Rec(lst);
}
}
return toret;
}
However this line will fail with:
List<object[]> serialisedProp = Serialise2D_Rec(lst);
With the error:
****: error CS0411: The type arguments for method '****.****.Serialise2D_Rec<T>(System.Collections.Generic.IEnumerable<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
How can I specify the type in the recursion, it seems that the syntax for dynamic generic types is not that trivial.