If you do not care to much about the structure of the string then the SoapFormatter is an option. Here is a quick and dirty example. Not pretty but it might work for you.
public static class Helpers
{
public static string ObjectToString(Array ar)
{
using (MemoryStream ms = new MemoryStream())
{
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(ms, ar);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
public static object ObjectFromString(string s)
{
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(s)))
{
SoapFormatter formatter = new SoapFormatter();
return formatter.Deserialize(ms) as Array;
}
}
public static T ObjectFromString<T>(string s)
{
return (T)Helpers.ObjectFromString(s);
}
}
These helpers can be used to transform any Serializable object to a string, including arrays, as long as the elements of the array are serializable.
// Serialize a 1 dimensional array to a string format
char[] ar = { '1', '2', '3' };
Console.WriteLine(Helpers.ObjectToString(ar));
// Serialize a 2 dimensional array to a string format
char[,] ar2 = {{ '1', '2', '3' },{ 'a', 'b', 'c' }};
Console.WriteLine(Helpers.ObjectToString(ar2));
// Deserialize an array from the string format
char[,] ar3 = Helpers.ObjectFromString(Helpers.ObjectToString(ar2)) as char[,];
char[,] ar4 = Helpers.ObjectFromString<char[,]>(Helpers.ObjectToString(ar2));