For a remoting scenario, the result would be very good to receive as an array or list of Tuple objects (among benefits being strong typing).
Example: dynamically convert SELECT Name, Age FROM Table => List<Tuple<string,int>>
Question: are there any samples out there that, given an arbitrary table of data (like SQL resultset or CSV file), with types of each column known only at runtime, to generate code that would dynamically create a strongly-typed List<Tuple<...>> object. Code should be dynamically generated, otherwise it would be extremely slow.
Edit: I changed the code to use the Tuple constructor instead of Tuple.Create. It currently works only for up to 8 values, but to add the ‘Tuple stacking’ should be trivial.
This is a little bit tricky and implementation is kind of dependent on the datasource.
To give an impression, I created a solution using a list of anonymous types as a source.
As Elion said, we need to dynamically create an expression tree to call it afterward. The basic technique we employ is called projection.
We have to get, at runtime the type information and create a ConstructorInfor of the Tuple(…) constructor according to the properties count. This is dynamic (although needs to be the same per record) per each call.
If you want to use a DataReader or some CVS input, you would need to rewrite the function GetTupleNewExpression.
I cant speak about the performance, although it should not be much slower as a native LINQ implementation as the generation of the LINQ expression only happens once per call.
If its too slow you could go down the road of generating code (and keep it stored in a file) for example using Mono.Cecil.
I couldn’t test this in C# 4.0 yet and but it should work.
If you want to try it in C# 3.5 you need the following code as well: