我想对项目的方法执行NUnit测试。这里 SerializeBinary()方法将对象转换为二进制流和 DeserializeFromBinaryStream()方法再次将其转换为先前相同的对象格式。现在我想在DesirializingFromBinary()方法任务之后检查这两个对象是否相同。 我从Here:开始,但没有明确我的概念。
我在下面附上我的部分代码。
第一部分:
public abstract class Streaming
{
private static int _streamingIDExponent = 41;
public const string MonthLookup = "ABCDEFGHIJK";
public const string ExpiryStringFormat = "ddMMMyyyy";
public const double DefaultTicksPerPoint = 3;
private long _StreamingID;
private int _minQty = DefaultMinimumQty;
public virtual void SerializeBinary(BinaryStreamWriter binaryStreamWriter)
{
binaryStreamWriter.Write(_StreamingID);
binaryStreamWriter.Write(_ex_StreamingID);
binaryStreamWriter.Write(_minQty);
binaryStreamWriter.Write(_extendedProperties.Count);
foreach (KeyValuePair<StreamingPropertyName, StreamingProperty> dictionaryEntry in _extendedProperties)
{
dictionaryEntry.Value.SerializeBinary(binaryStreamWriter);
}
}
public virtual bool DeserializeFromBinary(BinaryStreamReader binaryStreamReader, out string errorString)
{
errorString = string.Empty;
try
{
_StreamingID = binaryStreamReader.ReadInt64();
_exStreamingID = binaryStreamReader.ReadInt64();
_minQty = binaryStreamReader.ReadInt32();
}
catch (Exception oEx)
{
errorString = oEx.Message;
}
return string.IsNullOrEmpty(errorString);
}
第二部分:
public class Asset : Streaming
{
public override void SerializeBinary(BinaryStreamWriter binaryStreamWriter)
{
base.SerializeBinary(binaryStreamWriter);
}
public override bool DeserializeFromBinary(BinaryStreamReader binaryStreamReader, out string errorString)
{
if (!base.DeserializeFromBinary(binaryStreamReader, out errorString))
return false;
try
{
return true;
}
catch (Exception oEx)
{
errorString = oEx.Message;
return false;
}
}
第三部分:
public class BinaryStreamWriter : IDisposable
{
private BinaryWriter _binaryWriter;
public BinaryStreamWriter(BinaryWriter binaryWriter)
{
_binaryWriter = binaryWriter;
}
public void Write(object value)
{
if (value is Boolean) Write((Boolean)value);
else if (value is Byte) Write((Byte)value);
else if (value is Byte?) Write((Byte?)value);
else if (value is Byte[]) Write((Byte[])value);
else throw new InvalidOperationException(value.GetType().ToString());
}
void IDisposable.Dispose()
{
(_binaryWriter as IDisposable).Dispose();
}
}
请相应地建议我。