c# - 将 Int32 转换为 CultureInfo PowerShell 与 C#

标签 c# powershell casting

在 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 适用:

  1. 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/

相关文章:

c# - 查找一组字符串的第一个公共(public)子串

c# - Html.PasswordFor 没有填充?

powershell - 如何删除Powershell中的第一行和最后一行

c++ - 如何防止从 char 数组到 bool 的隐式转换

c# - Windows Phone - Color.FromArgb() 无效转换

java - 如何在 Java 中将 float 转换为整数

c# - 从空白启动时 VSTO 功能区不显示解决方案

c# - 重启后出现"A timeout was reached while waiting for the service to connect"错误

CSV 格式的 Powershell 组对象并将其导出

powershell - PSImageTools 无法加载 Wia.ImageFile