// Copyright (C) Stichting Deltares and State of the Netherlands 2023. 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.ComponentModel; using System.Globalization; using System.Linq; using System.Reflection; using Core.Common.Util.Attributes; namespace Core.Common.Util.Enums { /// /// A type converter to convert nullable Enum objects to and from various other representations. /// public class NullableEnumConverter : NullableConverter { /// /// Initializes a new instance of the class for the given nullable Enum . /// /// This class looks for the decorator. /// A that represents the type of enumeration to associate with this enumeration converter. /// Thrown when is not a nullable type. public NullableEnumConverter(Type type) : base(type) {} public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value == null) { throw new NotSupportedException("Cannot convert from (null)."); } var valueString = value as string; if (valueString != null) { FieldInfo fieldInfo = UnderlyingType.GetFields().FirstOrDefault(info => valueString == GetDisplayName(info)); if (fieldInfo != null) { return Enum.Parse(UnderlyingType, fieldInfo.Name); } } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType != typeof(string) || value == null) { return base.ConvertTo(context, culture, value, destinationType); } FieldInfo fieldInfo = UnderlyingType.GetField(value.ToString()); return GetDisplayName(fieldInfo); } /// /// Gets a collection of standard values for the data type this type converter is designed for. /// /// An that provides a format /// context that can be used to extract additional information about the environment /// from which this converter is invoked. This parameter or properties of this parameter can be null. /// A that holds a standard /// set of valid values, or null if the data type does not support a standard set of values. /// Does not add a value for null to the . public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { if (UnderlyingTypeConverter == null) { return base.GetStandardValues(context); } StandardValuesCollection values = UnderlyingTypeConverter.GetStandardValues(context); if (GetStandardValuesSupported(context) && values != null) { return new StandardValuesCollection(values); } return base.GetStandardValues(context); } private static string GetDisplayName(MemberInfo memberInfo) { var resourcesDisplayNameAttribute = (ResourcesDisplayNameAttribute) Attribute.GetCustomAttribute(memberInfo, typeof(ResourcesDisplayNameAttribute)); return resourcesDisplayNameAttribute?.DisplayName; } } }