using System; using System.ComponentModel; using System.Reflection; using Core.Common.Gui.Properties; namespace Core.Common.Gui.Attributes { /// /// Marks property as a conditional visible property. When this attribute is declared /// on a property, the declaring class should have a public method marked with /// to be used to evaluate if /// that property should be visible or not. /// /// /// /// This attribute provides a run-time alternative to . [AttributeUsage(AttributeTargets.Property)] public sealed class DynamicVisibleAttribute : Attribute { /// /// Determines whether the property is visible or not. /// /// The object. /// Name of the property of . /// True if the property is visible, false otherwise. /// /// does not correspond to a public property of . /// When there isn't a single method /// declared on marked with /// and/or isn't matching the signature defined by . public static bool IsVisible(object obj, string propertyName) { if (string.IsNullOrEmpty(propertyName)) { return BrowsableAttribute.Default.Browsable; } if (!IsPropertyDynamicallyVisible(obj, propertyName)) { return BrowsableAttribute.Default.Browsable; } var isPropertyVisibleDelegate = DynamicVisibleValidationMethodAttribute.CreateIsVisibleMethod(obj); return isPropertyVisibleDelegate(propertyName); } private static bool IsPropertyDynamicallyVisible(object obj, string propertyName) { MemberInfo propertyInfo = obj.GetType().GetProperty(propertyName); if (propertyInfo == null) { throw new MissingMemberException(string.Format(Resources.Could_not_find_property_0_on_type_1_, propertyName, obj.GetType())); } return IsDefined(propertyInfo, typeof(DynamicVisibleAttribute)); } } }