dri*_*iis 57 c# string-interpolation roslyn c#-6.0
C#6中的字符串插值让我写:
decimal m = 42.0m;
string x = $"The value is {m}";
Run Code Online (Sandbox Code Playgroud)
但是,字符串格式化的一个非常常见的用例是指定用于格式化值的语言环境.假设我需要InvariantCulture用于上面的格式化操作,那是什么语法?
这个讨论表明我应该能够做到这一点:
string x = INV($"The value is {m}");
Run Code Online (Sandbox Code Playgroud)
其中INV定义为
public static string INV(IFormattable formattable)
{
return formattable.ToString(null, System.Globalization.CultureInfo.InvariantCulture);
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.它编译,但它启动时我的程序挂在cmd.exe中 - 好像我假设正在调用klr.exe,挂起(编译器错误?)
这是VS15 CTP 6中的ASP.NET 5控制台项目.
pha*_*ing 80
你有什么应该工作.这是正确的语法."System.FormattableString"抽象类上还有一个方便的方法,它与建议的"INV"辅助方法具有相同的效果.
using static System.FormattableString;
...
string x = Invariant($"The value is {m}");
Run Code Online (Sandbox Code Playgroud)
dri*_*iis 17
我终于想通了.事实证明,编译器功能依赖于两种类型System.FormattableString,和System.Runtime.CompilerServices.FormattableStringFactory.这些不适用于我的项目 - 我猜他们可能还没有进入CTP6的所有平台.
这显然使编译器挂起如上所述.从CoreCLR代码中提取这两种类型的代码并将其添加到我的项目后,我的代码按预期工作.
这是通过InterpolationTests的代码注释得出的.Hooray的来源可用:-)
| 归档时间: |
|
| 查看次数: |
14648 次 |
| 最近记录: |