Skip to content

Instantly share code, notes, and snippets.

@millimoose
Created June 19, 2018 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save millimoose/58507be68ec34ea2490eae9b9a7300ab to your computer and use it in GitHub Desktop.
Save millimoose/58507be68ec34ea2490eae9b9a7300ab to your computer and use it in GitHub Desktop.
class DictFormatProvider : IFormatProvider, ICustomFormatter
{
IDictionary<string, object> _values;
public DictFormatProvider(IDictionary<string, object> values) {
_values = values;
}
public object GetFormat(Type type) {
return type == typeof(ICustomFormatter) ? this : null;
}
public string Format(string format, object arg, IFormatProvider formatProvider)
{
var value = _values[arg.ToString()];
if (value is IFormattable formattableValue)
{
return formattableValue.ToString(format, null);
}
else {
return value.ToString();
}
}
}
void Main()
{
FormattableString s = $"aaa{"foo":N}bbb";
var values = new Dictionary<string, object> {
["foo"] = 12345.67890
};
s.ToString(new DictFormatProvider(values)).Dump();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment