public static void AssertReadAndWrite <T>( IGeneric <T> value, T expected, int expectedLength, Action <ValuePair <T> > assertion = null) { var actualType = value .GetType(); if (typeof(T).IsArray) { expectedLength *= Marshal.SizeOf(typeof(T).GetMethod("Get").ReturnType); } var actual = (IGeneric <T>)Activator.CreateInstance(actualType); var memory = new MemoryStream(); var reader = new BinaryReader(memory); var writer = new BinaryWriter(memory); BinaryMapping.WriteObject(writer, value); Assert.Equal(expectedLength, memory.Length); memory.Position = 0; BinaryMapping.ReadObject(reader, actual); Assert.Equal(expectedLength, memory.Position); if (assertion != null) { assertion.Invoke(new ValuePair <T> { Expected = expected, Actual = actual.Value }); } else { Assert.Equal(expected, actual.Value); } }