StandardProperty.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. namespace InABox.Core
  6. {
  7. public class StandardProperty : IProperty
  8. {
  9. public Type _class;
  10. private Func<object, object> _getter;
  11. private string _name = "";
  12. private bool? _calculated;
  13. private Action<object, object?> _setter;
  14. public string _type = "";
  15. public Type _propertyType = typeof(object);
  16. public StandardProperty()
  17. {
  18. Editor = new NullEditor();
  19. HasEditor = false;
  20. }
  21. [ComboLookupEditor(typeof(StandardPropertyClassLookups))]
  22. public string Class
  23. {
  24. get => _class.EntityName();
  25. set
  26. {
  27. _class = CoreUtils.GetEntity(value);
  28. CheckExpressions();
  29. }
  30. }
  31. public Type? ClassType
  32. {
  33. get => _class;
  34. set
  35. {
  36. _class = value ?? typeof(object);
  37. }
  38. }
  39. public string Name
  40. {
  41. get => _name;
  42. set
  43. {
  44. _name = value;
  45. CheckExpressions();
  46. }
  47. }
  48. [ComboLookupEditor(typeof(PropertyTypeLookups))]
  49. public string Type
  50. {
  51. get => _type;
  52. set
  53. {
  54. Type propType;
  55. try
  56. {
  57. propType = CoreUtils.GetEntityOrNull(_type) ?? typeof(object);
  58. }
  59. catch
  60. {
  61. propType = typeof(object);
  62. }
  63. PropertyType = propType; // Invoke other setter to ensure consistency of functionality
  64. }
  65. }
  66. public Type PropertyType
  67. {
  68. get => _propertyType;
  69. set
  70. {
  71. _propertyType = value;
  72. _type = value.EntityName();
  73. IsEntityLink = _propertyType.GetInterfaces().Contains(typeof(IEntityLink));
  74. IsEnclosedEntity = _propertyType.GetInterfaces().Contains(typeof(IEnclosedEntity));
  75. }
  76. }
  77. public PropertyInfo Property { get; set; }
  78. //[ComboLookupEditor(typeof(PropertyEditorLookups))]
  79. public BaseEditor Editor { get; set; }
  80. public bool HasEditor { get; set; }
  81. public string Caption { get; set; }
  82. public long Sequence { get; set; }
  83. [NullEditor]
  84. public string Page { get; set; }
  85. public bool Required { get; set; }
  86. public LoggablePropertyAttribute? Loggable { get; set; }
  87. public IProperty? Parent { get; set; }
  88. public bool IsEntityLink { get; set; }
  89. public bool IsEnclosedEntity { get; set; }
  90. public bool IsCalculated
  91. {
  92. get
  93. {
  94. if (!_calculated.HasValue)
  95. {
  96. _calculated = Property.GetCustomAttribute<AggregateAttribute>() != null
  97. || Property.GetCustomAttribute<FormulaAttribute>() != null
  98. || Property.GetCustomAttribute<ConditionAttribute>() != null
  99. || Property.GetCustomAttribute<ChildEntityAttribute>() != null;
  100. }
  101. return _calculated.Value;
  102. }
  103. }
  104. public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute
  105. {
  106. return Property.GetCustomAttribute<TAttribute>();
  107. }
  108. public Expression Expression()
  109. {
  110. return CoreUtils.CreateMemberExpression(_class, Name);
  111. }
  112. public Func<object, object> Getter()
  113. {
  114. return _getter;
  115. }
  116. public Action<object, object?> Setter()
  117. {
  118. return _setter;
  119. }
  120. public override string ToString()
  121. {
  122. return string.Format("{0}.{1}", Class, Name);
  123. }
  124. private void CheckExpressions()
  125. {
  126. if (_class == null)
  127. return;
  128. if (string.IsNullOrEmpty(_name))
  129. return;
  130. try
  131. {
  132. _propertyType = CoreUtils.GetProperty(_class, _name).PropertyType;
  133. }
  134. catch (Exception e)
  135. {
  136. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  137. }
  138. try
  139. {
  140. _getter = Expressions.Getter(_class, _name);
  141. }
  142. catch (Exception e)
  143. {
  144. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  145. }
  146. try
  147. {
  148. _setter = Expressions.Setter(_class, _name);
  149. }
  150. catch (Exception e)
  151. {
  152. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  153. }
  154. }
  155. private class StandardPropertyClassLookups : LookupGenerator<object>
  156. {
  157. public StandardPropertyClassLookups(object[] items) : base(items)
  158. {
  159. var types = CoreUtils.TypeList(
  160. AppDomain.CurrentDomain.GetAssemblies(),
  161. myType =>
  162. myType.IsClass
  163. && !myType.IsAbstract
  164. && myType.IsSubclassOf(typeof(BaseEditor))
  165. );
  166. foreach (var type in types)
  167. AddValue(type.EntityName(), type.Name);
  168. }
  169. }
  170. }
  171. }