CultureInfo.CurrentCulture 可以为空吗?
空值会使我的程序崩溃,这是我不想要的。所以我在问,为了安全起见,我需要这样做吗?
var culture = CultureInfo.CurrentCulture ?? CultureInfo.InvariantCulture
CultureInfo.CurrentCulture 可以为空吗?
空值会使我的程序崩溃,这是我不想要的。所以我在问,为了安全起见,我需要这样做吗?
var culture = CultureInfo.CurrentCulture ?? CultureInfo.InvariantCulture
它绝对看起来肯定是非null
:
文化是执行线程的属性。此只读属性等效于检索该
CultureInfo
属性返回的对象Thread.CurrentCulture
。
Thread.CurrentCulture
如果您尝试将其设置为 ,则会引发异常null
,因此假设具有非null
值是不变量是合乎逻辑的。
除此之外,CultureInfo.CurrentCulture
给出确定其初始值的算法:
如何确定线程的文化
当一个线程启动时,它的文化最初确定如下:
DefaultThreadCurrentCulture
如果属性值不是 ,则通过检索由执行线程的应用程序域中的属性指定的区域性null
。通过调用 Windows
GetUserDefaultLocaleName
函数。
同样,这并没有打开一个null
值的选项。
不,保证非空。这是System.Threading.Thread.CurrentCulture
直接从CultureInfo.CurentCulture
(通过 ILSpy)返回的实现:
// System.Threading.Thread
public CultureInfo CurrentUICulture
{
[SecuritySafeCritical]
get
{
if (this.m_CurrentUICulture == null)
{
return CultureInfo.UserDefaultUICulture;
}
CultureInfo cultureInfo = null;
if (!Thread.nativeGetSafeCulture(this, Thread.GetDomainID(), true, ref cultureInfo) || cultureInfo == null)
{
return CultureInfo.UserDefaultUICulture;
}
return cultureInfo;
}
// setter following
因此,如果m_CurrentUICulture
为 null ,它将返回UserDefaultUICulture
。
这是来源:
internal static CultureInfo UserDefaultUICulture
{
get
{
CultureInfo cultureInfo = CultureInfo.s_userDefaultUICulture;
if (cultureInfo == null)
{
CultureInfo.s_userDefaultUICulture = CultureInfo.InvariantCulture;
cultureInfo = CultureInfo.InitUserDefaultUICulture();
CultureInfo.s_userDefaultUICulture = cultureInfo;
}
return cultureInfo;
}
}
如您所见,即使是 nullCultureInfo.InvariantCulture
也会返回。
查看 .NET 源代码,如果有人尝试将其设置为,它将引发错误null
- 所以不,它不能是null
.
/// <summary>Gets or sets the culture for the current thread.</summary>
/// <returns>A <see cref="T:System.Globalization.CultureInfo" /> representing the culture for the current thread.</returns>
/// <exception cref="T:System.NotSupportedException">The property is set to a neutral culture. Neutral cultures cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture.</exception>
/// <exception cref="T:System.ArgumentNullException">The property is set to null.</exception>
/// <filterpriority>2</filterpriority>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlThread" />
/// </PermissionSet>
[__DynamicallyInvokable]
public CultureInfo CurrentCulture
{
[__DynamicallyInvokable]
get
{
if (AppDomain.IsAppXModel())
{
return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? this.GetCurrentCultureNoAppX();
}
return this.GetCurrentCultureNoAppX();
}
[__DynamicallyInvokable, SecuritySafeCritical]
[SecurityPermission(SecurityAction.Demand, ControlThread = true)]
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
CultureInfo.nativeSetThreadLocale(value.SortName);
value.StartCrossDomainTracking();
this.m_CurrentCulture = value;
}
}