SerializableUtility.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Serialization
  8. * @{
  9. */
  10. /// <summary>
  11. /// Provides utility methods dealing with object serialization.
  12. /// </summary>
  13. public static class SerializableUtility
  14. {
  15. /// <summary>
  16. /// Clones the specified object. Non-serializable types and fields are ignored in clone. A deep copy is performed
  17. /// on all serializable elements except for resources or game objects.
  18. /// </summary>
  19. /// <param name="original">Non-null reference to the object to clone. Object type must be serializable.</param>
  20. /// <returns>Deep copy of the original object.</returns>
  21. public static object Clone(object original)
  22. {
  23. return Internal_Clone(original);
  24. }
  25. /// <summary>
  26. /// Creates an empty instance of the specified type.
  27. /// </summary>
  28. /// <typeparam name="T">Type of the object to create. Must be serializable.</typeparam>
  29. /// <returns>New instance of the specified type, or null if the type is not serializable.</returns>
  30. public static T Create<T>()
  31. {
  32. return (T)Internal_Create(typeof(T));
  33. }
  34. /// <summary>
  35. /// Creates an empty instance of the specified type.
  36. /// </summary>
  37. /// <param name="type">Type of the object to create. Must be serializable.</param>
  38. /// <returns>New instance of the specified type, or null if the type is not serializable.</returns>
  39. public static object Create(Type type)
  40. {
  41. return Internal_Create(type);
  42. }
  43. [MethodImpl(MethodImplOptions.InternalCall)]
  44. private static extern object Internal_Clone(object original);
  45. [MethodImpl(MethodImplOptions.InternalCall)]
  46. private static extern object Internal_Create(Type type);
  47. }
  48. /** @} */
  49. }