Add culture option to TypeConverterHelper, TextPrompt and AnsiConsole (#1014)

* Add culture option to TypeConverterHelper, TextPrompt and AnsiConsole

* Add IHasCulture interface
This commit is contained in:
Łukasz Sowa
2022-10-15 11:19:06 +02:00
committed by GitHub
parent 5f1121e8e1
commit 6a4d8c8f30
4 changed files with 82 additions and 9 deletions

View File

@@ -21,6 +21,28 @@ internal static class TypeConverterHelper
}
}
public static bool TryConvertFromStringWithCulture<T>(string input, CultureInfo? info, [MaybeNull] out T result)
{
try
{
if (info == null)
{
return TryConvertFromString<T>(input, out result);
}
else
{
result = (T)GetTypeConverter<T>().ConvertFromString(null!, info, input);
}
return true;
}
catch
{
result = default;
return false;
}
}
public static TypeConverter GetTypeConverter<T>()
{
var converter = TypeDescriptor.GetConverter(typeof(T));