Created
June 19, 2018 13:28
-
-
Save millimoose/58507be68ec34ea2490eae9b9a7300ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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