2
0

SerializableObject.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BansheeEngine
  10. {
  11. #pragma warning disable 649
  12. /// <summary>
  13. /// Allows you to access meta-data about a managed object and its fields. Similar to Reflection but simpler and faster.
  14. /// </summary>
  15. public sealed class SerializableObject : ScriptObject
  16. {
  17. internal SerializableProperty parentProperty;
  18. internal object parentObject;
  19. private SerializableField[] _fields;
  20. /// <summary>
  21. /// Creates a new serializable object for the specified object type.
  22. /// </summary>
  23. /// <param name="objectType">C# type of the object.</param>
  24. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  25. public SerializableObject(Type objectType, SerializableProperty parentProperty)
  26. {
  27. Internal_CreateInstance(this, objectType);
  28. this.parentProperty = parentProperty;
  29. this.parentObject = null;
  30. }
  31. /// <summary>
  32. /// Creates a new serializable object for the specified object type.
  33. /// </summary>
  34. /// <param name="objectType">C# type of the object.</param>
  35. /// <param name="parentObject">Specific instance of the object of <paramref name="objectType"/>.</param>
  36. public SerializableObject(Type objectType, object parentObject)
  37. {
  38. Internal_CreateInstance(this, objectType);
  39. this.parentProperty = null;
  40. this.parentObject = parentObject;
  41. }
  42. /// <summary>
  43. /// Creates a new serializable object for the specified object. Object must not be null.
  44. /// </summary>
  45. /// <param name="parentObject">Specific instance of the object.</param>
  46. public SerializableObject(object parentObject)
  47. {
  48. Internal_CreateInstance(this, parentObject.GetType());
  49. this.parentProperty = null;
  50. this.parentObject = parentObject;
  51. }
  52. /// <summary>
  53. /// Returns all fields in the object.
  54. /// </summary>
  55. public SerializableField[] Fields
  56. {
  57. get { return _fields; }
  58. }
  59. /// <summary>
  60. /// Returns the specific object instance this object is operating on.
  61. /// </summary>
  62. /// <returns>Object instance.</returns>
  63. public object GetReferencedObject()
  64. {
  65. if (parentProperty != null)
  66. return parentProperty.GetValue<object>();
  67. else
  68. return parentObject;
  69. }
  70. [MethodImpl(MethodImplOptions.InternalCall)]
  71. private static extern void Internal_CreateInstance(SerializableObject instance, Type objectType);
  72. }
  73. }