StandardProperty.cs 4.5 KB

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