UnityVariable.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Reflection;
  3. using Chronos.Reflection.Internal;
  4. using UnityObject = UnityEngine.Object;
  5. namespace Chronos.Reflection
  6. {
  7. [Serializable]
  8. public class UnityVariable : UnityMember
  9. {
  10. private enum SourceType
  11. {
  12. Unknown,
  13. Field,
  14. Property
  15. }
  16. private SourceType sourceType = SourceType.Unknown;
  17. /// <summary>
  18. /// The underlying reflected field, or null if the variable is a property.
  19. /// </summary>
  20. public FieldInfo fieldInfo { get; private set; }
  21. /// <summary>
  22. /// The underlying property field, or null if the variable is a field.
  23. /// </summary>
  24. public PropertyInfo propertyInfo { get; private set; }
  25. #region Constructors
  26. public UnityVariable() { }
  27. public UnityVariable(string name) : base(name) { }
  28. public UnityVariable(string name, UnityObject target) : base(name, target) { }
  29. public UnityVariable(string component, string name) : base(component, name) { }
  30. public UnityVariable(string component, string name, UnityObject target) : base(component, name, target) { }
  31. #endregion
  32. /// <inheritdoc />
  33. public override void Reflect()
  34. {
  35. EnsureAssigned();
  36. EnsureTargeted();
  37. fieldInfo = null;
  38. propertyInfo = null;
  39. sourceType = SourceType.Unknown;
  40. var memberInfo = UnityMemberHelper.ReflectVariable(reflectionTarget, name);
  41. fieldInfo = memberInfo as FieldInfo;
  42. propertyInfo = memberInfo as PropertyInfo;
  43. if (fieldInfo != null)
  44. {
  45. sourceType = SourceType.Field;
  46. }
  47. else if (propertyInfo != null)
  48. {
  49. sourceType = SourceType.Property;
  50. }
  51. isReflected = true;
  52. }
  53. /// <summary>
  54. /// Retrieves the value of the variable.
  55. /// </summary>
  56. public object Get()
  57. {
  58. EnsureReflected();
  59. switch (sourceType)
  60. {
  61. case SourceType.Field: return fieldInfo.GetValue(reflectionTarget);
  62. case SourceType.Property: return propertyInfo.GetValue(reflectionTarget, null);
  63. default: throw new UnityReflectionException();
  64. }
  65. }
  66. /// <summary>
  67. /// Retrieves the value of the variable casted to the specified type.
  68. /// </summary>
  69. public T Get<T>()
  70. {
  71. return (T)Get();
  72. }
  73. /// <summary>
  74. /// Assigns a new value to the variable.
  75. /// </summary>
  76. public void Set(object value)
  77. {
  78. EnsureReflected();
  79. switch (sourceType)
  80. {
  81. case SourceType.Field: fieldInfo.SetValue(reflectionTarget, value); break;
  82. case SourceType.Property: propertyInfo.SetValue(reflectionTarget, value, null); break;
  83. default: throw new UnityReflectionException();
  84. }
  85. }
  86. /// <summary>
  87. /// The type of the reflected field or property.
  88. /// </summary>
  89. public Type type
  90. {
  91. get
  92. {
  93. EnsureReflected();
  94. switch (sourceType)
  95. {
  96. case SourceType.Field: return fieldInfo.FieldType;
  97. case SourceType.Property: return propertyInfo.PropertyType;
  98. default: throw new UnityReflectionException();
  99. }
  100. }
  101. }
  102. public override bool Corresponds(UnityMember other)
  103. {
  104. return other is UnityVariable && base.Corresponds(other);
  105. }
  106. }
  107. }