StandardProperty.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. }
  100. return _calculated.Value;
  101. }
  102. }
  103. public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute
  104. {
  105. return Property.GetCustomAttribute<TAttribute>();
  106. }
  107. public Expression Expression()
  108. {
  109. return CoreUtils.CreateMemberExpression(_class, Name);
  110. }
  111. public Func<object, object> Getter()
  112. {
  113. return _getter;
  114. }
  115. public Action<object, object?> Setter()
  116. {
  117. return _setter;
  118. }
  119. public override string ToString()
  120. {
  121. return string.Format("{0}.{1}", Class, Name);
  122. }
  123. private void CheckExpressions()
  124. {
  125. if (_class == null)
  126. return;
  127. if (string.IsNullOrEmpty(_name))
  128. return;
  129. try
  130. {
  131. _propertyType = CoreUtils.GetProperty(_class, _name).PropertyType;
  132. }
  133. catch (Exception e)
  134. {
  135. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  136. }
  137. try
  138. {
  139. _getter = Expressions.Getter(_class, _name);
  140. }
  141. catch (Exception e)
  142. {
  143. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  144. }
  145. try
  146. {
  147. _setter = Expressions.Setter(_class, _name);
  148. }
  149. catch (Exception e)
  150. {
  151. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  152. }
  153. }
  154. private class StandardPropertyClassLookups : LookupGenerator<object>
  155. {
  156. public StandardPropertyClassLookups(object[] items) : base(items)
  157. {
  158. var types = CoreUtils.TypeList(
  159. AppDomain.CurrentDomain.GetAssemblies(),
  160. myType =>
  161. myType.IsClass
  162. && !myType.IsAbstract
  163. && myType.IsSubclassOf(typeof(BaseEditor))
  164. );
  165. foreach (var type in types)
  166. AddValue(type.EntityName(), type.Name);
  167. }
  168. }
  169. }
  170. }