StandardProperty.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 string Name
  32. {
  33. get => _name;
  34. set
  35. {
  36. _name = value;
  37. CheckExpressions();
  38. }
  39. }
  40. [ComboLookupEditor(typeof(PropertyTypeLookups))]
  41. public string Type
  42. {
  43. get => _type;
  44. set
  45. {
  46. Type propType;
  47. try
  48. {
  49. propType = CoreUtils.GetEntityOrNull(_type) ?? typeof(object);
  50. }
  51. catch
  52. {
  53. propType = typeof(object);
  54. }
  55. PropertyType = propType; // Invoke other setter to ensure consistency of functionality
  56. }
  57. }
  58. public Type PropertyType
  59. {
  60. get => _propertyType;
  61. set
  62. {
  63. _propertyType = value;
  64. _type = value.EntityName();
  65. IsEntityLink = _propertyType.GetInterfaces().Contains(typeof(IEntityLink));
  66. }
  67. }
  68. public PropertyInfo Property { get; set; }
  69. //[ComboLookupEditor(typeof(PropertyEditorLookups))]
  70. public BaseEditor Editor { get; set; }
  71. public bool HasEditor { get; set; }
  72. public string Caption { get; set; }
  73. public long Sequence { get; set; }
  74. [NullEditor]
  75. public string Page { get; set; }
  76. public bool Required { get; set; }
  77. public LoggablePropertyAttribute? Loggable { get; set; }
  78. public IProperty? Parent { get; set; }
  79. public bool IsEntityLink { get; set; }
  80. public bool IsCalculated
  81. {
  82. get
  83. {
  84. if (!_calculated.HasValue)
  85. {
  86. _calculated = Property.GetCustomAttribute<AggregateAttribute>() != null
  87. || Property.GetCustomAttribute<FormulaAttribute>() != null
  88. || Property.GetCustomAttribute<ConditionAttribute>() != null;
  89. }
  90. return _calculated.Value;
  91. }
  92. }
  93. public Expression Expression()
  94. {
  95. return CoreUtils.CreateMemberExpression(_class, Name);
  96. }
  97. public Func<object, object> Getter()
  98. {
  99. return _getter;
  100. }
  101. public Action<object, object> Setter()
  102. {
  103. return _setter;
  104. }
  105. public override string ToString()
  106. {
  107. return string.Format("{0}.{1}", Class, Name);
  108. }
  109. private void CheckExpressions()
  110. {
  111. if (_class == null)
  112. return;
  113. if (string.IsNullOrEmpty(_name))
  114. return;
  115. try
  116. {
  117. _propertyType = CoreUtils.GetProperty(_class, _name).PropertyType;
  118. }
  119. catch (Exception e)
  120. {
  121. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  122. }
  123. try
  124. {
  125. _getter = Expressions.Getter(_class, _name);
  126. }
  127. catch (Exception e)
  128. {
  129. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  130. }
  131. try
  132. {
  133. _setter = Expressions.Setter(_class, _name);
  134. }
  135. catch (Exception e)
  136. {
  137. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  138. }
  139. }
  140. private class StandardPropertyClassLookups : LookupGenerator<object>
  141. {
  142. public StandardPropertyClassLookups(object[] items) : base(items)
  143. {
  144. var types = CoreUtils.TypeList(
  145. AppDomain.CurrentDomain.GetAssemblies(),
  146. myType =>
  147. myType.IsClass
  148. && !myType.IsAbstract
  149. && myType.IsSubclassOf(typeof(BaseEditor))
  150. );
  151. foreach (var type in types)
  152. AddValue(type.EntityName(), type.Name);
  153. }
  154. }
  155. }
  156. }