public static bool TryGetValue <T>(this IIniSection iniSection, string propertyName, out T propertyValue) { IIniProperty property = iniSection.GetProperty(propertyName); // Only attempt to convert the string if the property isn't empty, because some types will get an unexpected value (e.g. bools will be false). if (!string.IsNullOrWhiteSpace(property?.Value)) { try { Type convertType = typeof(T); if (convertType.IsNullableType()) { convertType = Nullable.GetUnderlyingType(convertType); } object convertedPropertyValue = Convert.ChangeType(property.Value, convertType, CultureInfo.InvariantCulture); if (!(convertedPropertyValue is null)) { propertyValue = (T)convertedPropertyValue; return(true); } } catch (FormatException) { } } propertyValue = default; return(false); }
public static bool TryGetValue <T>(this IIniSection iniSection, string propertyName, out T propertyValue) { IIniProperty property = iniSection.GetProperty(propertyName); // Only attempt to convert the string if the property isn't empty, because some types will get an unexpected value (e.g. bools will be false). if (!string.IsNullOrWhiteSpace(property?.Value)) { try { return(TypeUtilities.TryCast(property.Value, out propertyValue)); } catch (FormatException) { } } propertyValue = default; return(false); }