/* * Copyright (c) 2025 Proton AG * * This file is part of ProtonVPN. * * ProtonVPN 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. * * ProtonVPN 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 ProtonVPN. If not, see . */ namespace ProtonVPN.Common.Core.Extensions; public static class EnumExtensions { /// /// Converts the string representation of the name or numeric value of one or more /// enumerated constants to an equivalent enumerated object. In case of failure /// the return value is null. /// /// The enumeration type to which to convert value. /// The string representation of the enumeration name or underlying value to convert. /// The object of type TEnum if the value parameter was converted successfully; otherwise, null. public static TEnum? ToEnumOrNull(this string value) where TEnum : struct { bool isParseSuccessful = Enum.TryParse(value, out TEnum result); return isParseSuccessful ? result : null; } /// /// Converts the string representation of the name or numeric value of one or more /// enumerated constants to an equivalent enumerated object. In case of failure /// the method throws. /// /// The enumeration type to which to convert value. /// The string representation of the enumeration name or underlying value to convert. /// The object of type TEnum if the value parameter public static TEnum ToEnum(this string value) where TEnum : struct { return (TEnum)Enum.Parse(typeof(TEnum), value); } }