// Copyright (C) Stichting Deltares 2022. 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 Core.Common.Util.Attributes; using Core.Common.Util.Exceptions; namespace Core.Common.Util.Enums { /// /// 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(nameof(value), @"An Enum type value is required."); } if (!typeof(Enum).IsAssignableFrom(typeof(T))) { throw new InvalidTypeParameterException(@"The type parameter has to be an Enum type.", nameof(T)); } Value = value; SetDisplayName(value); } /// /// The actual value of that has been wrapped. /// public T Value { get; } /// /// 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; } public override string ToString() { return DisplayName; } private void SetDisplayName(T value) { DisplayName = new EnumTypeConverter(typeof(T)).ConvertToString(value) ?? value.ToString(); } } }