// Copyright (C) Stichting Deltares 2019. All rights reserved. // // This file is part of Riskeer. // // Riskeer 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.Reflection; namespace Core.Common.Gui.Converters { /// /// Attribute when using the to define what is /// shown as name and value for each element. /// [AttributeUsage(AttributeTargets.Property)] public class KeyValueElementAttribute : Attribute { /// /// Creates a new instance of . /// /// The name of the property to show as name. /// The name of the property to show as value. /// Thrown when any parameter is null. public KeyValueElementAttribute(string namePropertyName, string valuePropertyName) { NamePropertyName = namePropertyName; ValuePropertyName = valuePropertyName; if (valuePropertyName == null) { throw new ArgumentNullException(nameof(valuePropertyName)); } if (namePropertyName == null) { throw new ArgumentNullException(nameof(namePropertyName)); } } /// /// Gets the property value from the that is used /// as name. /// /// The source to obtain the property value of. /// The value used as name of the property. /// Thrown when the property used for the name of /// the is not found on the . /// public virtual string GetName(object source) { PropertyInfo namePropertyInfo = source.GetType().GetProperty(NamePropertyName); if (namePropertyInfo == null) { throw new ArgumentException($"Name property '{NamePropertyName}' was not found on type {source.GetType().Name}."); } return Convert.ToString(namePropertyInfo.GetValue(source, new object[0])); } /// /// Gets the property value from the that is used /// as value. /// /// The source to obtain the property value of. /// The value used as value of the property. /// Thrown when the property used for the value of /// the is not found on the . /// public virtual string GetValue(object source) { PropertyInfo valuePropertyInfo = source.GetType().GetProperty(ValuePropertyName); if (valuePropertyInfo == null) { throw new ArgumentException($"Value property '{ValuePropertyName}' was not found on type {source.GetType().Name}."); } return Convert.ToString(valuePropertyInfo.GetValue(source, new object[0])); } /// /// Gets the name of the property to show as value. /// protected string NamePropertyName { get; } /// /// Gets the name of the property to show as value. /// protected string ValuePropertyName { get; } } }