Suppose I want to write an extension method to dump some data from a T[,] to a CSV:
public static void WriteCSVData<T>(this T[,] data, StreamWriter sw)
{
for (int row = 0; row < data.GetLength(0); row++)
for (int col = 0; col < data.GetLength(1); col++)
{
string s = data[row, col].ToString();
if (s.Contains(","))
sw.Write("\"" + s + "\"");
else
sw.Write(s);
if (col < data.GetLength(1) - 1)
sw.Write(",");
else
sw.WriteLine();
}
}
which I could call with
using (StreamWriter sw = new StreamWriter("data.csv"))
myData.WriteCSVData(sw);
but suppose myData is a Complex[,] and I want to write the magnitude of the complex number, not the full value. It would be handy if I could write:
using (StreamWriter sw = new StreamWriter("data.csv"))
myData.WriteCSVData(sw, d => d.Magnitude);
but I'm not sure how to implement that in the extension method or if it's even possible.