SerializableObject.cs 3.0 KB

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