public void IGenericSharedAPI_SerializeDeserialize(int count)
{
IEnumerable expected = NonGenericIEnumerableFactory(count);
if (!SupportsSerialization)
{
// No assert for !IsSerializable here, as some collections are only serializable sometimes,
// e.g. HybridDictionary.Keys is serializable when using Hashtable.Keys but not when
// using ListDictionary.Keys.
return;
}
Assert.True(expected.GetType().GetTypeInfo().IsSerializable, "Expected IsSerializable");
var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, expected);
ms.Position = 0;
IEnumerable actual = (IEnumerable)bf.Deserialize(ms);
if (Order == EnumerableOrder.Sequential)
{
Assert.Equal(expected, actual);
}
else
{
var expectedSet = new HashSet<object>(expected.Cast<object>());
var actualSet = new HashSet<object>(actual.Cast<object>());
Assert.Subset(expectedSet, actualSet);
Assert.Subset(actualSet, expectedSet);
}
}
}