Co.*_*den 4 .net c# resources c#-6.0
我需要$"test message {variable1} and {variable2}"在资源文件中添加一种字符串。
早些时候,这可以使用如下所示的字符串构建器格式来完成
test message {0} and {1}
Run Code Online (Sandbox Code Playgroud)
让我知道是否有任何替代方法可以为 $ 字符串格式执行此操作
$"test message {variable1} and {variable2}"是 的语法糖string.Format("test message {0} and {1}", variable1, variable2)。
您只能在资源中放置静态值。您不能将动态值作为来自动态值的格式化字符串。
如果您真的想为此使用字符串插值,则需要使用格式来键入资源并执行以下操作:
string R(FormattableString fs)
{
return string.Format(Resources.GetString(fs.Format), fs.GetArguments());
}
// uses "test message {0} and {1}" as resource key
R($"test message {variable1} and {variable2}");
Run Code Online (Sandbox Code Playgroud)