public Exception Deserialize(Stream source)
{
if (source == null)
throw new ArgumentNullException("source");
Exception exception = null;
var flag = source.ReadByte();
var typeAliasUsed = ((flag & 1) != 0);
var typeSerializable = ((flag & 2) != 0);
Type type;
if (typeAliasUsed)
{
var typeAlias = source.Read32BitEncodedInt();
type = _data.TypeTable.GetType(typeAlias);
if (type == null)
{
exception = new InvalidOperationException(
"Cannot resolve type from alias. Alias=" + typeAlias);
}
}
else
{
var typeName = source.ReadString();
type = TypeUtility.GetType(typeName);
if (type == null)
{
exception = new InvalidOperationException(
"Cannot resolve type from name. Name=" + typeName);
}
}
if (exception == null)
{
exception = (Exception)Activator.CreateInstance(type);
if (typeSerializable)
{
int dataLen = source.Read32BitEncodedInt();
_data.MessageSerializer.Deserialize(source, exception, type, dataLen);
}
}
else
{
if (typeSerializable)
{
int dataLen = source.Read32BitEncodedInt();
source.Position = source.Position + dataLen;
}
}
return exception;
}
}