Get Substitutions before executing the format method #140
-
Is there a way from the library itself, to get the list of substitutions (like the names) prior to executing the format? If there is no existing method I will result to regular expression, but it would be nice if the library had the support so it would provide the substitutions without any control or conditional elements. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
If you don't mind executing the formatter with an empty data source, you could get all existing placeholders like shown in the code below. class Program
{
private static readonly List<string> _placeholders = new List<string>();
static void Main(string[] args)
{
Smart.Default.Settings.FormatErrorAction = ErrorAction.Ignore;
Smart.Default.Settings.ParseErrorAction = ErrorAction.Ignore;
Smart.Default.OnFormattingFailure += DefaultOnOnFormattingFailure;
var result = Smart.Format("{few} {placeholders} {that} {cannot} {be} {found}", new Dictionary<string, string>());
Console.WriteLine(result);
foreach (var placeholder in _placeholders)
{
Console.WriteLine(placeholder);
}
}
private static void DefaultOnOnFormattingFailure(object? sender, FormattingErrorEventArgs e)
{
_placeholders.Add(e.Placeholder);
}
} Output:
|
Beta Was this translation helpful? Give feedback.
If you don't mind executing the formatter with an empty data source, you could get all existing placeholders like shown in the code below.