I have few methods that returns different Generic Lists.
Exists in .net any class static method or whatever to convert any list into a datatable? The only thing that i can imagine is use Reflection to do this.
IF i have this:
List<Whatever> whatever = new List<Whatever>();
(This next code doesn't work of course, but i would like to have the possibility of:
DataTable dt = (DataTable) whatever;
I've written a small library myself to accomplish this task. It uses reflection only for the first time an object type is to be translated to a datatable. It emits a method that will do all the work translating an object type.
Its blazing fast. You can find it here: ModelShredder on GoogleCode
Another approach is the above:
if you have properties in your class this line of code is OK !!
but if you have all public fields then use this:
the original answer is from above , I just edited to use fields instead of properties
and to use it do this
try this
If you are using VB.NET then this class does the job.