Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ObjectId.FromObject.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System.IO;
4 using SiliconStudio.Core.Serialization;
5 
6 namespace SiliconStudio.Core.Storage
7 {
8  /// <summary>
9  /// A hash to uniquely identify data.
10  /// </summary>
11  public partial struct ObjectId
12  {
13  /// <summary>
14  /// Computes a hash from an object using <see cref="BinarySerializationWriter"/>.
15  /// </summary>
16  /// <param name="obj">The object.</param>
17  /// <returns>The hash of the object.</returns>
18  public static ObjectId FromObject<T>(T obj)
19  {
20  byte[] buffer;
21  return FromObject(obj, out buffer);
22  }
23 
24  /// <summary>
25  /// Computes a hash from an object using <see cref="BinarySerializationWriter" />.
26  /// </summary>
27  /// <typeparam name="T">The type of the object to serialize</typeparam>
28  /// <param name="obj">The object.</param>
29  /// <param name="buffer">The buffer containing the serialized object.</param>
30  /// <returns>The hash of the object.</returns>
31  public static ObjectId FromObject<T>(T obj, out byte[] buffer)
32  {
33  var stream = new MemoryStream();
34  var writer = new BinarySerializationWriter(stream);
35  writer.Serialize(ref obj, ArchiveMode.Serialize);
36  stream.Position = 0;
37  buffer = stream.ToArray();
38  return FromBytes(buffer);
39  }
40  }
41 }
Implements SerializationStream as a binary writer.
A hash to uniquely identify data.
Definition: ObjectId.cs:13