[英]How can I check the validation of the LCID without try\catch block?
某些类型具有TryParse
方法,例如Int32
, Int64
, Boolean
等。它允许在没有try \\ catch块的情况下检查字符串值。 当一个周期中处理许多不正确的值时,这对生产率有很大影响。 我需要对LCID的字符串值执行相同的操作。 但是CultureInfo
类没有TryParse
方法。
CultureInfo culture = null;
try {
culture = CultureInfo.GetCultureInfo(Convert.ToInt32(lcid, 16));
}
catch {
}
我该如何重写此代码?
您可以通过LCID将所有CultureInfo对象缓存在字典中:
var culturesByLcid = CultureInfo
.GetCultures(CultureTypes.AllCultures)
.ToDictionary(c => c.LCID, c => c);
并使用TryGetValue像:
CultureInfo found;
if (culturesByLcid.TryGetValue(lcid, out found))
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.