Very curious.
However attempting to remote an array of interfaces does not seem like a very 
useful thing to do.  When the array is deserialized, what objects would .NET be 
expected to create?  Since IComparable is not an object, simply an interface, 
some real concrete object must be created.  There are many System objects 
implementing this interface (double, float, int, string, ...); in that example 
code it would be valid for deserialization to construct any of these are 
concrete objects to obtain the IComparable interfaces required to satisfy 
construction of the requested IComparable[] arr2.

One solution, referring to the example using the BinaryFormater, is to convert 
(box) the values being remoted to String objects as in:

MemoryStream m = new MemoryStream();
             System.Runtime.Serialization.Formatters.Binary.BinaryFormatter 
formatter = new 
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
             IComparable[] arr = new IComparable[1];
             arr[0] = Convert.ToString(123.45);
             formatter.Serialize(m, arr);
             m.Seek(0, SeekOrigin.Begin);
             IComparable[] arr2 = (IComparable[])formatter.Deserialize(m);
             Console.WriteLine(arr2[0].ToString() + " " + 
arr2[0].GetType().ToString());


A better solution would be to always remote arrays of objects that support the 
IComparable interface, instead of interfaces:

MemoryStream m = new MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new 
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
object [] arrx = new object[3];
arrx[0] = 123.45;
arrx[1] = "122.46";
arrx[2] = 121.47f;
formatter.Serialize(m, arrx);
m.Seek(0, SeekOrigin.Begin);
object[] arr2 = (object[])formatter.Deserialize(m);
foreach(object oC in arr2)
{
   Console.WriteLine(oC.ToString() + " " + oC.GetType().ToString());
   Console.WriteLine("   " + 
oC.GetType().GetInterface("System.IComparable").ToString());
}
Console.ReadLine();


-- Neal

Reply via email to