SerializableObject.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #pragma warning disable 649
  11. /// <summary>
  12. /// Allows you to access meta-data about a managed object and its fields. Similar to Reflection but simpler and faster.
  13. /// </summary>
  14. public sealed class SerializableObject : ScriptObject
  15. {
  16. internal SerializableProperty parentProperty;
  17. internal object parentObject;
  18. private SerializableField[] _fields;
  19. /// <summary>
  20. /// Creates a new serializable object for the specified object type.
  21. /// </summary>
  22. /// <param name="objectType">C# type of the object.</param>
  23. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  24. public SerializableObject(Type objectType, SerializableProperty parentProperty)
  25. {
  26. Internal_CreateInstance(this, objectType);
  27. this.parentProperty = parentProperty;
  28. this.parentObject = null;
  29. }
  30. /// <summary>
  31. /// Creates a new serializable object for the specified object type.
  32. /// </summary>
  33. /// <param name="objectType">C# type of the object.</param>
  34. /// <param name="parentObject">Specific instance of the object of <paramref name="objectType"/>.</param>
  35. public SerializableObject(Type objectType, object parentObject)
  36. {
  37. Internal_CreateInstance(this, objectType);
  38. this.parentProperty = null;
  39. this.parentObject = parentObject;
  40. }
  41. /// <summary>
  42. /// Creates a new serializable object for the specified object. Object must not be null.
  43. /// </summary>
  44. /// <param name="parentObject">Specific instance of the object.</param>
  45. public SerializableObject(object parentObject)
  46. {
  47. Internal_CreateInstance(this, parentObject.GetType());
  48. this.parentProperty = null;
  49. this.parentObject = parentObject;
  50. }
  51. /// <summary>
  52. /// Returns all fields in the object.
  53. /// </summary>
  54. public SerializableField[] Fields
  55. {
  56. get { return _fields; }
  57. }
  58. /// <summary>
  59. /// Returns the specific object instance this object is operating on.
  60. /// </summary>
  61. /// <returns>Object instance.</returns>
  62. public object GetReferencedObject()
  63. {
  64. if (parentProperty != null)
  65. return parentProperty.GetValue<object>();
  66. else
  67. return parentObject;
  68. }
  69. [MethodImpl(MethodImplOptions.InternalCall)]
  70. private static extern void Internal_CreateInstance(SerializableObject instance, Type objectType);
  71. }
  72. /** @} */
  73. }