4 using System.Collections.Generic;
6 using System.Runtime.CompilerServices;
8 namespace SiliconStudio.Core.Serialization
10 using SiliconStudio.Core.Reflection;
17 public static T Clone<T>(T obj)
19 var memoryStream =
new MemoryStream();
21 writer.SerializeExtended(obj, ArchiveMode.Serialize, null);
24 T result =
default(T);
25 memoryStream.Seek(0, SeekOrigin.Begin);
27 reader.SerializeExtended(ref result, ArchiveMode.Deserialize, null);
39 [MethodImpl(MethodImplOptions.AggressiveInlining)]
53 [MethodImpl(MethodImplOptions.AggressiveInlining)]
65 [MethodImpl(MethodImplOptions.AggressiveInlining)]
68 T result =
default(T);
69 stream.Serialize(ref result, ArchiveMode.Deserialize);
78 [MethodImpl(MethodImplOptions.AggressiveInlining)]
91 [MethodImpl(MethodImplOptions.AggressiveInlining)]
94 DataSerializer<T> dataSerializer = stream.Context.SerializerSelector.GetSerializer<T>();
95 if (dataSerializer == null)
96 throw new InvalidOperationException(
string.Format(
"Could not find serializer for type {0}.", typeof(T)));
98 dataSerializer.PreSerialize(ref obj, mode, stream);
99 dataSerializer.Serialize(ref obj, mode, stream);
107 [MethodImpl(MethodImplOptions.AggressiveInlining)]
111 stream.Serialize(ref value);
120 [MethodImpl(MethodImplOptions.AggressiveInlining)]
124 stream.Serialize(ref value);
133 [MethodImpl(MethodImplOptions.AggressiveInlining)]
137 stream.Serialize(ref value);
146 [MethodImpl(MethodImplOptions.AggressiveInlining)]
150 stream.Serialize(ref value);
159 [MethodImpl(MethodImplOptions.AggressiveInlining)]
163 stream.Serialize(ref value);
172 [MethodImpl(MethodImplOptions.AggressiveInlining)]
176 stream.Serialize(ref value);
185 [MethodImpl(MethodImplOptions.AggressiveInlining)]
189 stream.Serialize(ref value);
198 [MethodImpl(MethodImplOptions.AggressiveInlining)]
202 stream.Serialize(ref value);
211 [MethodImpl(MethodImplOptions.AggressiveInlining)]
215 stream.Serialize(ref value);
224 [MethodImpl(MethodImplOptions.AggressiveInlining)]
228 stream.Serialize(ref value);
237 [MethodImpl(MethodImplOptions.AggressiveInlining)]
241 stream.Serialize(ref value);
250 [MethodImpl(MethodImplOptions.AggressiveInlining)]
254 stream.Serialize(ref value);
263 [MethodImpl(MethodImplOptions.AggressiveInlining)]
267 stream.Serialize(ref value);
276 [MethodImpl(MethodImplOptions.AggressiveInlining)]
279 byte[] value =
new byte[
count];
280 stream.Serialize(value, 0,
count);
290 [MethodImpl(MethodImplOptions.AggressiveInlining)]
293 stream.Serialize(ref value);
303 [MethodImpl(MethodImplOptions.AggressiveInlining)]
306 stream.Serialize(ref value);
316 [MethodImpl(MethodImplOptions.AggressiveInlining)]
319 stream.Serialize(ref value);
329 [MethodImpl(MethodImplOptions.AggressiveInlining)]
332 stream.Serialize(ref value);
342 [MethodImpl(MethodImplOptions.AggressiveInlining)]
345 stream.Serialize(ref value);
355 [MethodImpl(MethodImplOptions.AggressiveInlining)]
358 stream.Serialize(ref value);
368 [MethodImpl(MethodImplOptions.AggressiveInlining)]
371 stream.Serialize(ref value);
381 [MethodImpl(MethodImplOptions.AggressiveInlining)]
384 stream.Serialize(ref value);
394 [MethodImpl(MethodImplOptions.AggressiveInlining)]
397 stream.Serialize(ref value);
407 [MethodImpl(MethodImplOptions.AggressiveInlining)]
410 stream.Serialize(ref value);
420 [MethodImpl(MethodImplOptions.AggressiveInlining)]
423 stream.Serialize(ref value);
433 [MethodImpl(MethodImplOptions.AggressiveInlining)]
436 stream.Serialize(ref value);
446 [MethodImpl(MethodImplOptions.AggressiveInlining)]
449 stream.Serialize(ref value);
463 [MethodImpl(MethodImplOptions.AggressiveInlining)]
466 stream.Serialize(values, offset,
count);
static uint ReadUInt32(this SerializationStream stream)
Reads a 4-byte unsigned integer from the stream.
static SerializationStream Write(this SerializationStream stream, int value)
Writes a 4-byte signed integer to the specified stream.
static SerializationStream Write(this SerializationStream stream, byte[] values, int offset, int count)
Writes a byte array region to the specified stream.
static SerializationStream Write(this SerializationStream stream, double value)
Writes a 8-byte floating point value to the specified stream.
static char ReadChar(this SerializationStream stream)
Reads a unicode character from the stream.
static int ReadInt32(this SerializationStream stream)
Reads a 4-byte signed integer from the stream.
static byte[] ReadBytes(this SerializationStream stream, int count)
Reads the specified number of bytes.
Various useful extension methods on top of SerializationStream for serialization/deserialization of c...
static SerializationStream Write(this SerializationStream stream, sbyte value)
Writes a signed byte to the specified stream.
static string ReadString(this SerializationStream stream)
Reads a string.
static SerializationStream Write(this SerializationStream stream, short value)
Writes a 2-byte signed integer to the specified stream.
Implements SerializationStream as a binary writer.
static double ReadDouble(this SerializationStream stream)
Reads a 8-byte floating point value from the stream.
static float ReadSingle(this SerializationStream stream)
Reads a 4-byte floating point value from the stream.
static byte ReadByte(this SerializationStream stream)
Reads a unsigned byte integer from the stream.
static SerializationStream Write(this SerializationStream stream, long value)
Writes a 8-byte signed integer to the specified stream.
Base class for implementation of SerializationStream.
static ushort ReadUInt16(this SerializationStream stream)
Reads a 2-byte unsigned integer from the stream.
static SerializationStream Write(this SerializationStream stream, byte value)
Writes an unsigned byte to the specified stream.
static SerializationStream Write(this SerializationStream stream, string value)
Writes a string to the specified stream.
Implements SerializationStream as a binary reader.
static SerializationStream Write(this SerializationStream stream, char value)
Writes a unicode character to the specified stream.
static short ReadInt16(this SerializationStream stream)
Reads a 2-byte signed integer from the stream.
static long ReadInt64(this SerializationStream stream)
Reads a 8-byte signed integer from the stream.
static ulong ReadUInt64(this SerializationStream stream)
Reads a 8-byte unsigned integer from the stream.
static bool ReadBoolean(this SerializationStream stream)
Reads a boolean value from the stream.
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
Describes how to serialize and deserialize an object of a given type.
static SerializationStream Write(this SerializationStream stream, bool value)
Writes a boolean value to the specified stream.
static SerializationStream Write(this SerializationStream stream, ushort value)
Writes a 2-byte unsigned integer to the specified stream.
static SerializationStream Write(this SerializationStream stream, ulong value)
Writes a 8-byte unsigned integer to the specified stream.
static SerializationStream Write(this SerializationStream stream, float value)
Writes a 4-byte floating point value to the specified stream.
static sbyte ReadSByte(this SerializationStream stream)
Reads a signed byte from the stream.
static SerializationStream Write(this SerializationStream stream, uint value)
Writes a 4-byte unsigned integer to the specified stream.