. NET encoding process, when we open a new thread, its default CurrentCulture property is the current system of culture, in the absence of the completion of software localization, the use of the default system of cultural encoding, may lead to software in the interface display, numbers, symbols, Time formatting and other aspects of the exception, resulting in software run errors. For example, in Chinese and English systems, the dot number is generally used as the delimiter for double type data, and comma is used as a separator in Russian system.
If you want to change its CurrentCulture property , it is generally done by modifying the Thread.CurrentCulture value. However, when there are more new threads, it is very troublesome to set them each time, which is prone to omissions. To this end, the author consulted a lot of literature and network blog, found a common default CultureInfo property setting scheme.
When the. NET environment is framework4.5 and above, CultureInfo provides two static properties Defaultthreadculture and Defaultthreaduiculture, one modification to implement all of the non-explicit settings The default value is used by the thread.currentculture thread.
two private static fields can be set by reflection when the. NET environment is a framework4.0 versions_userdefaultculture and s_userdefaultuiculture to achieve.
When the. NET environment is framework4.0 the following versions,Two private static fields M can be set by reflection_userdefaultculture and M_userdefaultuiculture to achieve.
< Span style= "FONT-SIZE:14PX;" >
1 usingSystem.Globalization;2 usingSystem.Reflection;3 4 Public voidSetdefaultculture (CultureInfo culture)5 {6Type type =typeof(CultureInfo);7 8 Try9 {TenType. InvokeMember ("s_userdefaultculture", OneBindingflags.setfield | BindingFlags.NonPublic |BindingFlags.Static, A NULL, - Culture, - New Object[] {culture}); the -Type. InvokeMember ("s_userdefaultuiculture", -Bindingflags.setfield | BindingFlags.NonPublic |BindingFlags.Static, - NULL, + Culture, - New Object[] {culture}); + } A Catch { } at - Try - { -Type. InvokeMember ("m_userdefaultculture", -Bindingflags.setfield | BindingFlags.NonPublic |BindingFlags.Static, - NULL, in Culture, - New Object[] {culture}); to +Type. InvokeMember ("m_userdefaultuiculture", -Bindingflags.setfield | BindingFlags.NonPublic |BindingFlags.Static, the NULL, * Culture, $ New Object[] {culture});Panax Notoginseng } - Catch { } the}
In the code, the Try-catch module is used to wrap the two reflection settings in order to ensure its universality, in less than framewor4.5 version of the program, only need to add the above code, call once to achieve multi-threaded CultureInfo property settings, effectively avoid the regional cultural differences caused by errors.
Resources:
1, https://www.rastating.com/setting-default-currentculture-in-all-versions-of-net/
2, Https://msdn.microsoft.com/zh-cn/library/26etazsy%28v=vs.110%29.aspx#FormatProviders
Set up. NET multithreading default CultureInfo