假设,在C#程序中,我的app.config中有以下几行
<appSettings>
<add key="FormattedString" value="{greeting}, {name}." />
</appSettings>而且,在我的代码中,我使用它的方式如下:
private void doStuff()
{
var toBeFormatted = ConfigurationManager.AppSettings["FormattedString"];
string greeting = @"Hi There";
string name = @"Bob";
}我希望使用toBeFormatted变量作为FormattableString,以便能够通过字符串插值插入变量--大致如下所示:
Console.WriteLine(toBeFormatted);我试过这样的事情:
var toBeFormatted = $ConfigurationManager.AppSettings["FormattedString"];或
Console.WriteLine($toBeFormatted);但两者都会导致错误。有没有办法让编译器知道toBeFormatted字符串应该用作FormattableString
发布于 2018-03-21 16:08:03
如果不是这样的话,我建议以下简单的解决方案:
<appSettings>
<add key="FormattedString" value="{0}, {1}." />
</appSettings>然后在您的代码中:
Console.WriteLine(string.Format(toBeFormatted,greeting, name));https://stackoverflow.com/questions/49411116
复制相似问题