本文整理汇总了C#中System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.UnsafeDeserialize方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryFormatter.UnsafeDeserialize方法的具体用法?C# BinaryFormatter.UnsafeDeserialize怎么用?C# BinaryFormatter.UnsafeDeserialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
的用法示例。
在下文中一共展示了BinaryFormatter.UnsafeDeserialize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SerializationUnsafeRoundtrip
public void SerializationUnsafeRoundtrip ()
{
Stream s = GetSerializedStream ();
BinaryFormatter bf = new BinaryFormatter ();
SerializationTest clone = (SerializationTest) bf.UnsafeDeserialize (s, null);
Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
Assert.IsFalse (clone.Boolean, "Boolean");
}
示例2: FromStream
//.........这里部分代码省略.........
{
pdn21Format = false;
break;
}
}
// Read in the header if we found the 'magic' bytes identifying a PDN 2.1 file
XmlDocument headerXml = null;
if (pdn21Format)
{
// This is a Paint.NET v2.1+ file.
int low = stream.ReadByte();
if (low == -1)
{
throw new EndOfStreamException();
}
int mid = stream.ReadByte();
if (mid == -1)
{
throw new EndOfStreamException();
}
int high = stream.ReadByte();
if (high == -1)
{
throw new EndOfStreamException();
}
int byteCount = low + (mid << 8) + (high << 16);
byte[] bytes = new byte[byteCount];
int bytesRead = Utility.ReadFromStream(stream, bytes, 0, byteCount);
if (bytesRead != byteCount)
{
throw new EndOfStreamException("expected " + byteCount + " bytes, but only got " + bytesRead);
}
string xml = Encoding.UTF8.GetString(bytes);
headerXml = new XmlDocument();
headerXml.LoadXml(xml);
}
else
{
stream.Position = oldPosition; // rewind and try as v2.0-or-earlier file
}
// Start reading the data section of the file. Determine if it's gzip or regular
long oldPosition2 = stream.Position;
int first = stream.ReadByte();
if (first == -1)
{
throw new EndOfStreamException();
}
int second = stream.ReadByte();
if (second == -1)
{
throw new EndOfStreamException();
}
Document document;
object docObject;
BinaryFormatter formatter = new BinaryFormatter();
SerializationFallbackBinder sfb = new SerializationFallbackBinder();
sfb.AddAssembly(Assembly.GetExecutingAssembly()); // first try PaintDotNet.Data.dll
sfb.AddAssembly(typeof(Utility).Assembly); // second, try PdnLib.dll
sfb.AddAssembly(typeof(SystemLayer.Memory).Assembly); // third, try PaintDotNet.SystemLayer.dll
formatter.Binder = sfb;
if (first == 0 && second == 1)
{
DeferredFormatter deferred = new DeferredFormatter();
formatter.Context = new StreamingContext(formatter.Context.State, deferred);
docObject = formatter.UnsafeDeserialize(stream, null);
deferred.FinishDeserialization(stream);
}
else if (first == 0x1f && second == 0x8b)
{
stream.Position = oldPosition2; // rewind to the start of 0x1f, 0x8b
GZipStream gZipStream = new GZipStream(stream, CompressionMode.Decompress, true);
docObject = formatter.UnsafeDeserialize(gZipStream, null);
}
else
{
throw new FormatException("file is not a valid Paint.NET document");
}
document = (Document)docObject;
document.Dirty = true;
document.headerXml = headerXml;
document.Invalidate();
return document;
}
示例3: UnsafeDeserialization_PermitOnly_SerializationFormatter
public void UnsafeDeserialization_PermitOnly_SerializationFormatter ()
{
BinaryFormatter bf = new BinaryFormatter ();
SerializationTest clone = (SerializationTest) bf.UnsafeDeserialize (stream, null);
Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
Assert.IsFalse (clone.Boolean, "Boolean");
}