SerializableObject.cs 2.9 KB

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