// 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 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 General Public License for more details. // // You should have received a copy of the GNU 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 Core.Common.Utils.Attributes; using Core.Common.Utils.Exceptions; namespace Core.Common.Utils { /// /// This class wraps a Enum value of type so that a display name can be /// obtained for that value. /// /// The enum type to wrap. public class EnumDisplayWrapper { /// /// Creates a new instance of . /// /// The enum value to wrap. /// Thrown when /// is null. /// Thrown when /// is not an Enum type. public EnumDisplayWrapper(T value) { if (value == null) { throw new ArgumentNullException("value", "An Enum type value is required."); } if (!(typeof(Enum).IsAssignableFrom(typeof(T)))) { throw new InvalidTypeParameterException("T", "The type parameter has to be an Enum type."); } Value = value; SetDisplayName(value); } /// /// The actual value of that has been wrapped. /// public T Value { get; private set; } /// /// Returns the name to use to display the enum value. /// /// The display name of the enum value or the default string representation of the value /// if no was defined for the enum value. public string DisplayName { get; private set; } private void SetDisplayName(T value) { var enumField = typeof(T).GetField(Enum.GetName(typeof(T), value)); var displayName = (ResourcesDisplayNameAttribute) Attribute.GetCustomAttribute(enumField, typeof(ResourcesDisplayNameAttribute)); DisplayName = displayName == null ? value.ToString() : displayName.DisplayName; } } }