SerializableUtility.cs 1.9 KB

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