using System; using System.Linq.Expressions; using System.Runtime.CompilerServices; namespace InABox.Core { public interface IProperty { string Class { get; set; } Type? ClassType { get; set; } string Name { get; set; } string Type { get; set; } Type PropertyType { get; set; } string Page { get; set; } /// /// Whether the property or any parents actually declares an editor. /// /// /// If false, will be a . /// bool HasEditor { get; set; } BaseEditor Editor { get; set; } long Sequence { get; set; } string Caption { get; set; } bool IsCalculated { get; } bool IsDBColumn { get; } /// /// An is required if it has the defined on it.
/// If it is part of an , then it is only required if the property on the parent class /// also has . ///
bool Required { get; set; } /// /// Null if the is not loggable.
/// An is loggable if it has the defined on it.
/// If it is part of an , then it is only loggable if the property on the parent class /// also has . ///
LoggablePropertyAttribute? Loggable { get; set; } IProperty? Parent { get; set; } bool IsEntityLink { get; set; } bool IsEnclosedEntity { get; set; } bool IsParent { get; set; } Expression Expression(); Func Getter(); Action Setter(); TAttribute? GetAttribute() where TAttribute : Attribute; decimal PropertySequence(); } public static class PropertyExtensions { public static bool HasAttribute(this IProperty property) where TAttribute : Attribute => property.GetAttribute() != null; /// /// Get the outermost parent property which has an editor. /// /// /// public static IProperty? GetParentWithEditor(this IProperty property) { if (property.Parent == null) return null; var parent = property.Parent.GetParentWithEditor(); if (parent != null) return parent; if (property.Parent.HasEditor) { return property.Parent; } return null; } /// /// Gets the outermost parent property which matches the predicate. /// /// /// /// public static IProperty? GetOuterParent(this IProperty property, Func predicate) { if (property.Parent == null) return null; return property.Parent.GetOuterParent(predicate) ?? (predicate(property.Parent) ? property.Parent : null); } /// /// Gets the innermost parent property which matches the predicate. /// /// /// /// public static IProperty? GetParent(this IProperty property, Func predicate) { if (property.Parent == null) return null; if(predicate(property.Parent)) return property.Parent; return property.Parent.GetParent(predicate); } public static bool HasParentEditor(this IProperty property) { return property.Parent != null && (property.Parent.HasEditor || property.Parent.HasParentEditor()); } public static bool HasParentEntityLink(this IProperty property) { return property.Parent != null && (property.Parent.IsEntityLink || property.Parent.HasParentEntityLink()); } public static bool ShouldShowEditor(this IProperty property) { if (property.Parent == null) return true; if (property.HasParentEditor()) return false; if (property.Parent.IsEntityLink && !property.Name.EndsWith(".ID")) return false; if (property.Parent.HasParentEntityLink()) return false; return true; } } }