代码之家  ›  专栏  ›  技术社区  ›  Amit Bisht

资源中的c#$string

  •  4
  • Amit Bisht  · 技术社区  · 7 年前

    $"test message {variable1} and {variable2}" 资源文件中的字符串类型。
    在前面,可以使用下面的字符串生成器格式来完成此操作

    test message {0} and {1}  
    

    1 回复  |  直到 7 年前
        1
  •  5
  •   Paulo Morgado    7 年前

    $"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}");