本文整理汇总了C#中ISerializationContext.CreateBlock方法的典型用法代码示例。如果您正苦于以下问题:C# ISerializationContext.CreateBlock方法的具体用法?C# ISerializationContext.CreateBlock怎么用?C# ISerializationContext.CreateBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISerializationContext
的用法示例。
在下文中一共展示了ISerializationContext.CreateBlock方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Serialize
/// <summary>
/// Serializes a tag structure into a context.
/// </summary>
/// <param name="context">The serialization context to use.</param>
/// <param name="tagStructure">The tag structure.</param>
public void Serialize(ISerializationContext context, object tagStructure, uint? offset = null)
{
// Serialize the structure to a data block
var info = new TagStructureInfo(tagStructure.GetType(), _version);
context.BeginSerialize(info);
var tagStream = new MemoryStream();
var structBlock = context.CreateBlock();
SerializeStruct(context, tagStream, structBlock, info, tagStructure);
// Finalize the block and write all of the tag data out
var mainStructOffset = offset.HasValue ? offset.Value : structBlock.Finalize(tagStream);
var data = tagStream.ToArray();
context.EndSerialize(info, data, mainStructOffset);
}
示例2: SerializeTagBlock
/// <summary>
/// Serializes a tag block.
/// </summary>
/// <param name="context">The serialization context to use.</param>
/// <param name="tagStream">The stream to write completed blocks of tag data to.</param>
/// <param name="block">The temporary block to write incomplete tag data to.</param>
/// <param name="list">The list of values in the tag block.</param>
/// <param name="listType">Type of the list.</param>
/// <param name="valueInfo">Information about the value. Can be <c>null</c>.</param>
private void SerializeTagBlock(ISerializationContext context, MemoryStream tagStream, IDataBlock block, object list, Type listType, TagFieldAttribute valueInfo)
{
var writer = block.Writer;
var listCount = 0;
if (list != null)
{
// Use reflection to get the number of elements in the list
var countProperty = listType.GetProperty("Count");
listCount = (int)countProperty.GetValue(list);
}
if (listCount == 0)
{
writer.Write(0);
writer.Write(0);
writer.Write(0);
return;
}
// Serialize each value in the list to a data block
var tagBlock = context.CreateBlock();
var enumerableList = (System.Collections.IEnumerable)list;
var valueType = listType.GenericTypeArguments[0];
foreach (var val in enumerableList)
SerializeValue(context, tagStream, tagBlock, val, null, valueType);
// Ensure the block is aligned correctly
var align = Math.Max(DefaultBlockAlign, (valueInfo != null) ? valueInfo.DataAlign : 0);
StreamUtil.Align(tagStream, (int)align);
// Finalize the block and write the tag block reference
writer.Write(listCount);
block.WritePointer(tagBlock.Finalize(tagStream), listType);
writer.Write(0);
}
示例3: SerializeIndirectValue
private void SerializeIndirectValue(ISerializationContext context, MemoryStream tagStream, IDataBlock block, object val, Type valueType)
{
var writer = block.Writer;
if (val == null)
{
writer.Write(0);
return;
}
// Serialize the value to a temporary block
var valueBlock = context.CreateBlock();
SerializeValue(context, tagStream, valueBlock, val, null, valueType);
// Finalize the block and write the pointer
block.WritePointer(valueBlock.Finalize(tagStream), valueType);
}