I have written a method that converts a generic list to a DataTable using FastMember from NuGet.
This is my code:
public DataTable ConvertGenericListToDataTable(List<CustomObject> inputList)
{
var dt = new DataTable();
using (var reader = ObjectReader.Create(inputList))
{
dt.Load(reader);
}
return dt;
}
var customObject = new List<CustomObject>();
var dt = ListToDataTable.ConvertGenericListToDataTable(customObject);
Which works fine.
Customobject is a custom object i have created, i have several different lists that i want to pass to my method: List<CustomobjectA>
or List<CustomobjectB>
and so on.
Its not much of a problem writing a method for every type of list i want to convert to a DataTable, but this could end in repeating the same lines of code over and over again, this is something i obviously want to prevent
I tried changing the parameter's type to List<object>
and List<dynamic>
.
Then my code won't compile because: "The best overloadmethod match for ConvertGenericListToDataTable has some invalid arguments".
Is there a way i can pass a List of objects as a parameter without defining the exact type of the object ?
What about having a generic ConvertGenericListToDataTable
method?
public DataTable ConvertGenericListToDataTable<T>(List<T> inputList)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With