SerializableObject.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. /// Returns all fields in the object.
  42. /// </summary>
  43. public SerializableField[] Fields
  44. {
  45. get { return _fields; }
  46. }
  47. /// <summary>
  48. /// Returns the specific object instance this object is operating on.
  49. /// </summary>
  50. /// <returns>Object instance.</returns>
  51. public object GetReferencedObject()
  52. {
  53. if (parentProperty != null)
  54. return parentProperty.GetValue<object>();
  55. else
  56. return parentObject;
  57. }
  58. [MethodImpl(MethodImplOptions.InternalCall)]
  59. private static extern void Internal_CreateInstance(SerializableObject instance, Type objectType);
  60. }
  61. }