在 PowerShell 中我可以使用以下语法:
PS C:\> [globalization.cultureinfo]1049
LCID Name DisplayName
---- ---- -----------
1049 ru-RU Russian (Russia)
PS C:\> [globalization.cultureinfo]1033
LCID Name DisplayName
---- ---- -----------
1033 en-US English (United States)
PS C:\> [globalization.cultureinfo]1080
LCID Name DisplayName
---- ---- -----------
1080 fo-FO Faroese (Faroe Islands)
但我无法使用 C# 实现相同的转换:
var eng = (CultureInfo)1033;
给出编译时错误:
Error CS0030 Cannot convert type 'int' to 'System.Globalization.CultureInfo'
它在 PowerShell 中如何工作以及如何在 C# 中执行相同操作?
最佳答案
PowerShell 隐式调用 CultureInfo(Int32) constructor ,即new CultureInfo(1049)
。
PowerShell 的转换规则已记录在案 on MSDN 。对于您的情况,规则 5 适用:
- Constructor conversion. If the target type defines a constructor that takes your input, use that.
证明:
PS> Trace-Command TypeConversion { [Globalization.CultureInfo]1049 | Out-Null } -PSHost
DEBUG: TypeConversion Information: 0 : Constructor result: "ru-RU".
关于c# - 将 Int32 转换为 CultureInfo PowerShell 与 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41060314/