本文整理汇总了C#中ISerializer.Deserialize方法的典型用法代码示例。如果您正苦于以下问题:C# ISerializer.Deserialize方法的具体用法?C# ISerializer.Deserialize怎么用?C# ISerializer.Deserialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISerializer
的用法示例。
在下文中一共展示了ISerializer.Deserialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestSerializer
// generic test for any serializer implementation
private void TestSerializer(ISerializer<LogEntry<TestOperation>> serializer)
{
var testMessage = new TestOperation
{
StringValue = "STRING",
BoolValue = true,
IntValue = 42,
ArrayValue = new int[] { 2, 4, 6, 8 }
};
var testEntry = new LogEntry<TestOperation>(new LogEntryId(1, 2), testMessage);
using (var stream = new MemoryStream())
{
stream.Position.Should().Be(0);
stream.Length.Should().Be(0);
// write the object to the stream
serializer.Serialize(testEntry, stream);
stream.Position.Should().NotBe(0);
stream.Length.Should().NotBe(0);
// read the object from the stream
stream.Position = 0;
var clone = serializer.Deserialize(stream);
stream.Position.Should().NotBe(0);
clone.Should().NotBeNull();
clone.Id.Term.Should().Be(1);
clone.Id.Index.Should().Be(2);
clone.Operation.Should().NotBeNull();
clone.Operation.StringValue.Should().Be("STRING");
clone.Operation.BoolValue.Should().BeTrue();
clone.Operation.IntValue.Should().Be(42);
clone.Operation.ArrayValue.Should().HaveCount(4);
// write the clone to the stream (as a second entry)
clone.Operation.StringValue = "MODIFIED";
serializer.Serialize(clone, stream);
// read the stream back, we expect to find 2 entries
// reading beyond the end of the stream will result in nulls
stream.Position = 0;
var clone1 = serializer.Deserialize(stream);
var clone2 = serializer.Deserialize(stream);
var clone3 = serializer.Deserialize(stream);
clone1.Operation.Should().NotBeNull();
clone2.Operation.Should().NotBeNull();
clone3.Operation.Should().BeNull();
clone1.Operation.StringValue.Should().Be("STRING");
clone2.Operation.StringValue.Should().Be("MODIFIED");
}
}
示例2: TestSerialization
void TestSerialization(ISerializer serializer)
{
DummSerializableClass dummyTestClass = new DummSerializableClass();
dummyTestClass.BoolProperty = true;
dummyTestClass.IntProperty = 5;
dummyTestClass.LongProperty = (long)int.MaxValue * 2;
dummyTestClass.DecimalProperty = (decimal)5.1;
dummyTestClass.DoubleProperty = 5.3;
dummyTestClass.StringProperty = "test";
dummyTestClass.SubClassProperty = new DummySubClass() { Id = 15, Name = "test name" };
dummyTestClass.ListProperty = new List<DummySubClass>();
dummyTestClass.ListProperty.Add(new DummySubClass() { Id = 200, Name = "child 1 test name" });
dummyTestClass.ListProperty.Add(new DummySubClass() { Id = 201, Name = "child 2 test name" });
DummSerializableClass deserialized = (DummSerializableClass)serializer.Deserialize(serializer.Serialize(dummyTestClass));
Assert.AreNotEqual(dummyTestClass, deserialized);
Assert.AreEqual(dummyTestClass.BoolProperty, deserialized.BoolProperty);
Assert.AreEqual(dummyTestClass.IntProperty, deserialized.IntProperty);
Assert.AreEqual(dummyTestClass.LongProperty, deserialized.LongProperty);
Assert.AreEqual(dummyTestClass.DecimalProperty, deserialized.DecimalProperty);
Assert.AreEqual(dummyTestClass.DoubleProperty, deserialized.DoubleProperty);
Assert.AreEqual(dummyTestClass.StringProperty, deserialized.StringProperty);
Assert.AreEqual(dummyTestClass.SubClassProperty.Name, deserialized.SubClassProperty.Name);
Assert.AreEqual(dummyTestClass.SubClassProperty.Id, deserialized.SubClassProperty.Id);
Assert.AreEqual(dummyTestClass.ListProperty.Count(), deserialized.ListProperty.Count());
Assert.AreEqual(dummyTestClass.ListProperty[0].Id, deserialized.ListProperty[0].Id);
Assert.AreEqual(dummyTestClass.ListProperty[0].Name, deserialized.ListProperty[0].Name);
Assert.AreEqual(dummyTestClass.ListProperty[1].Id, deserialized.ListProperty[1].Id);
Assert.AreEqual(dummyTestClass.ListProperty[1].Name, deserialized.ListProperty[1].Name);
}
示例3: From
public static ParameterExt From(ParameterData data, ISerializer serializer)
{
var para = new ParameterExt();
para.Type = TypeUtils.GetType(data.TypeName);
para.Value = serializer.Deserialize(para.Type, data.Data);
return para;
}
示例4: ToConsumedMessage
internal ConsumedMessage ToConsumedMessage(ISerializer serializer, MessageBinding messageBinding)
{
return new ConsumedMessage(serializer.Deserialize(_args.Body,
messageBinding.RuntimeType,
_args.BasicProperties.CreateEncoding()),
_args);
}
示例5: Content
private static ConsumedMessage Content(BasicDeliverEventArgs args,
ISerializer serializer,
MessageType messageType)
{
return new ConsumedMessage(serializer.Deserialize(args.Body,
messageType.RuntimeType,
Encoding(args)),
args);
}
示例6: ReadRequestDto
private Object ReadRequestDto(HttpListenerRequest request, ISerializer serializer)
{
var convertTo = LookupTargetType(request);
Object requestDto;
using (var stream = request.InputStream)
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
requestDto = serializer.Deserialize(reader, convertTo);
}
return requestDto;
}
示例7: WinMsgDataGram
private WinMsgDataGram(IntPtr lpParam, ISerializer serializer)
{
serializer.Requires("serializer").IsNotNull();
this.serializer = serializer;
allocatedMemory = false;
dataStruct = (Native.COPYDATASTRUCT) Marshal.PtrToStructure(lpParam, typeof (Native.COPYDATASTRUCT));
var bytes = new byte[dataStruct.cbData];
Marshal.Copy(dataStruct.lpData, bytes, 0, dataStruct.cbData);
string rawmessage;
using (var stream = new MemoryStream(bytes))
{
var b = new BinaryFormatter();
rawmessage = (string) b.Deserialize(stream);
}
dataGram = serializer.Deserialize<DataGram>(rawmessage);
}
示例8: ExecuteStreamInternal
/// <summary>
///
/// </summary>
/// <param name="requestStream"></param>
/// <param name="productName"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public static Stream ExecuteStreamInternal(Stream requestStream, string productName, ISerializer serializer) {
XdsResponseDocument xdsResponse = null;
MemoryStream result = null;
try {
Guard.Assert(requestStream != null && requestStream.Length > 0, "요청 정보가 없습니다.");
CheckProductExists(productName);
var xdsRequest = (XdsRequestDocument)serializer.Deserialize(requestStream.ToBytes());
xdsResponse = XmlDataServiceFacade.Execute(xdsRequest, productName);
}
catch(Exception ex) {
if(log.IsErrorEnabled)
log.ErrorException("예외가 발생했습니다.", ex);
if(xdsResponse == null)
xdsResponse = new XdsResponseDocument();
xdsResponse.ReportError(ex);
}
finally {
result = new MemoryStream(serializer.Serialize(xdsResponse));
}
return result;
}
示例9: Parse
public static object Parse(ISerializer serializer, string envelopeStr)
{
var envelope = serializer.Deserialize<Envelope>(envelopeStr);
return serializer.Deserialize(envelope.Content, envelope.Type);
}
示例10: DeserializeFrom
public virtual void DeserializeFrom(byte[] serializedObj, ISerializer serializer)
{
IDictionary dict = (IDictionary)serializer.Deserialize(serializedObj);
FromDictionary(dict);
}
示例11: CreateDictionaryData
/// <summary>
/// Creates an DictionaryData instance and fill it with serialized object.
/// </summary>
public static DictionaryData CreateDictionaryData(byte[] serializedObj, ISerializer serializer)
{
IDictionary dict = (IDictionary)serializer.Deserialize(serializedObj);
return CreateDictionaryData(dict);
}
示例12: GetContext
public IDataContext GetContext(ISerializer serializer)
{
if (_context == null)
{
if (_bytes == null)
return DataContext.Empty;
using (var ms = new MemoryStream(_bytes))
_context = (IDataContext)serializer.Deserialize(ms);
}
return _context;
}
示例13: readFile
private ObjectStoreEntry readFile(string fname, ISerializer serializer, DateTime now, ref long priorLoadSize, Stopwatch clock)
{
using (var fs = new FileStream(fname, FileMode.Open))//, FileAccess.Read, FileShare.Read, 64*1024, FileOptions.SequentialScan))
{
var entry = new ObjectStoreEntry();
try
{
entry.Value = serializer.Deserialize(fs);
}
catch (Exception error)
{
WriteLog(MessageType.Error, "Deserialization error: " + error.Message, fname, FROM);
return null;
}
Interlocked.Add( ref m_LoadSize, fs.Length);
if (m_LoadSize-priorLoadSize > 32*1024*1024)
{
WriteLog(MessageType.Info, string.Format("Loaded disk bytes {0} in {1}", LoadSize, clock.Elapsed), null, FROM);
priorLoadSize = m_LoadSize;
}
entry.Key = getGUIDFromFileName(fname);
entry.LastTime = now;
entry.Status = ObjectStoreEntryStatus.Normal;
return entry;
}
}
示例14: Open
/// <summary>
///
/// </summary>
/// <param name="stream"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public static Project Open(Stream stream, ISerializer serializer)
{
using (var archive = new ZipArchive(stream, ZipArchiveMode.Read))
{
var projectEntry = archive.Entries.FirstOrDefault(e => e.FullName == ProjectEntryName);
if (projectEntry == null)
return null;
var project = default(Project);
// First step is to read project entry and deserialize project object.
using (var entryStream = projectEntry.Open())
{
string json = ReadUtf8Text(entryStream);
project = serializer.Deserialize<Project>(json);
}
// Second step is to read (if any) project images.
foreach (var entry in archive.Entries)
{
if (entry.FullName.StartsWith(ImageEntryNamePrefix))
{
using (var entryStream = entry.Open())
{
var bytes = ReadBinary(entryStream);
project.AddImage(entry.FullName, bytes);
}
}
else
{
// Ignore all other entries.
}
}
return project;
}
}
示例15: Deserialize
public object Deserialize(ISerializer serializer, BinaryReader binary)
{
var type = binary.ReadTypeCode();
switch (type)
{
case TypeCode.Array:
{
var arrayType = TypeSpecifier.Read(binary);
var rank = binary.ReadInt32();
var lengths = new Int32[rank];
for (var i = 0; i < rank; ++i)
{
lengths[i] = binary.ReadInt32();
}
var array = Array.CreateInstance(arrayType.GetElementType(), lengths);
var indexer = new Int32[array.Rank];
while (!IsEnd(array, indexer))
{
var value = serializer.Deserialize(serializer, binary);
array.SetValue(value, indexer);
Next(array, indexer);
}
return array;
}
case TypeCode.String:
{
var length = binary.ReadInt32();
var bytes = binary.ReadBytes(length);
return System.Text.Encoding.ASCII.GetString(bytes);
}
case TypeCode.List:
{
var collectionType = TypeSpecifier.Read(binary);
var length = binary.ReadInt32();
var collection = (IList)Activator.CreateInstance(collectionType);
for (var i = 0; i < length; ++i)
{
collection.Add(serializer.Deserialize(serializer, binary));
}
return collection;
}
case TypeCode.Dictionary:
var dictionaryType = TypeSpecifier.Read(binary);
var numberOfPairs = binary.ReadInt32();
var dictionary = (IDictionary)Activator.CreateInstance(dictionaryType);
for (var i = 0; i < numberOfPairs; ++i)
{
var key = serializer.Deserialize(serializer, binary);
var value = serializer.Deserialize(serializer, binary);
dictionary[key] = value;
}
return dictionary;
case TypeCode.Object:
// XXX: HACK
var position = binary.BaseStream.Position;
var objectType = TypeSpecifier.Read(binary);
binary.BaseStream.Seek(position, SeekOrigin.Begin);
return CustomBinarySerializer.Serializers.ForObject(objectType).Deserialize(serializer, binary);
case TypeCode.Int64:
return binary.ReadInt64();
case TypeCode.Double:
return binary.ReadDouble();
case TypeCode.Int16:
return binary.ReadInt16();
case TypeCode.Int32:
return binary.ReadInt32();
case TypeCode.Byte:
return binary.ReadByte();
}
throw new ArgumentException(type.ToString());
}