// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.ComponentModel; using System.Globalization; using System.Reflection; using Core.Common.Gui.Properties; namespace Core.Common.Gui.Attributes { /// /// Marks property as a conditional read-only 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 read-only or not. /// /// /// /// This attribute provides a run-time alternative to . [AttributeUsage(AttributeTargets.Property)] public sealed class DynamicReadOnlyAttribute : Attribute { /// /// Determines whether the property is read-only or not. /// /// The object. /// The name of the property of . /// True if the property is read-only, false otherwise. /// Thrown when /// does not correspond to a public property of . /// Thrown when there isn't a single method /// declared on marked with /// that is matching the signature defined by . public static bool IsReadOnly(object obj, string propertyName) { if (string.IsNullOrEmpty(propertyName)) { return ReadOnlyAttribute.Default.IsReadOnly; } if (!IsPropertyDynamicallyReadOnly(obj, propertyName)) { return ReadOnlyAttribute.Default.IsReadOnly; } var isPropertyReadOnlyDelegate = DynamicReadOnlyValidationMethodAttribute.CreateIsReadOnlyMethod(obj); return isPropertyReadOnlyDelegate(propertyName); } private static bool IsPropertyDynamicallyReadOnly(object obj, string propertyName) { MemberInfo propertyInfo = obj.GetType().GetProperty(propertyName); if (propertyInfo == null) { throw new MissingMemberException(string.Format(CultureInfo.CurrentCulture, Resources.Could_not_find_property_0_on_type_1_, propertyName, obj.GetType())); } return IsDefined(propertyInfo, typeof(DynamicReadOnlyAttribute)); } } }