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;
Here's a nice 2013 update using FastMember from NuGet:
This uses the FastMember's meta-programming API for maximum performance. If you want to restrict it to particular members (or enforce the order), then you can do that too:
Editor's Dis/claimer: FastMember is a Marc Gravell project. Its gold and full-on flys!
Yes, this is pretty much the exact opposite of this one; reflection would suffice - or if you need quicker,
HyperDescriptor
in 2.0, or maybeExpression
in 3.5. Actually,HyperDescriptor
should be more than adequate.For example:
Now with one line you can make this many many times faster than reflection (by enabling
HyperDescriptor
for the object-typeT
).edit re performance query; here's a test rig with results:
I suspect that the bottleneck has shifted from member-access to
DataTable
performance... I doubt you'll improve much on that...code: