2 // System.Globalization.RegionInfo.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.CompilerServices;
31 using System.Runtime.InteropServices;
32 using System.Threading;
34 namespace System.Globalization
36 [System.Runtime.InteropServices.ComVisible(true)]
38 [StructLayout (LayoutKind.Sequential)]
39 public partial class RegionInfo
41 static RegionInfo currentRegion;
43 public static RegionInfo CurrentRegion {
45 var region = currentRegion;
47 CultureInfo ci = CultureInfo.CurrentCulture;
49 region = new RegionInfo (ci);
52 region = CreateFromNSLocale ();
56 if (Interlocked.CompareExchange (ref currentRegion, region, null) != null)
57 region = currentRegion;
64 // the following (instance) fields must be _first_ and stay synchronized
65 // with the mono's MonoRegionInfo defined in mono/metadata/object-internals.h
66 #pragma warning disable 649
73 string currencySymbol;
74 string isoCurrencySymbol;
75 string currencyEnglishName;
76 string currencyNativeName;
77 #pragma warning restore 649
79 public RegionInfo (int culture)
81 if (!GetByTerritory (CultureInfo.GetCultureInfo (culture)))
82 throw new ArgumentException (
83 String.Format ("Region ID {0} (0x{0:X4}) is not a supported region.", culture), "culture");
86 public RegionInfo (string name)
89 throw new ArgumentNullException ();
91 if (construct_internal_region_from_name (name.ToUpperInvariant ())) {
94 if (!GetByTerritory (CultureInfo.GetCultureInfo (name)))
95 throw new ArgumentException (String.Format ("Region name {0} is not supported.", name), "name");
98 RegionInfo (CultureInfo ci)
100 if (ci.LCID == CultureInfo.InvariantCultureId) {
105 nativeName = englishName = "Invariant Country";
106 currencySymbol = "\u00A4";
107 isoCurrencySymbol ="XDR";
108 currencyEnglishName = currencyNativeName = "International Monetary Fund";
112 if (ci.Territory == null)
113 throw new NotImplementedException ("Neutral region info");
115 construct_internal_region_from_name (ci.Territory.ToUpperInvariant ());
118 bool GetByTerritory (CultureInfo ci)
121 throw new Exception ("INTERNAL ERROR: should not happen.");
122 if (ci.IsNeutralCulture || ci.Territory == null)
125 return construct_internal_region_from_name (ci.Territory.ToUpperInvariant ());
128 [MethodImplAttribute (MethodImplOptions.InternalCall)]
129 private extern bool construct_internal_region_from_name (string name);
131 [System.Runtime.InteropServices.ComVisible(false)]
132 public virtual string CurrencyEnglishName {
133 get { return currencyEnglishName; }
136 public virtual string CurrencySymbol {
137 get { return currencySymbol; }
140 [MonoTODO ("DisplayName currently only returns the EnglishName")]
141 public virtual string DisplayName {
142 get { return englishName; }
145 public virtual string EnglishName {
146 get { return englishName; }
149 [System.Runtime.InteropServices.ComVisible(false)]
150 public virtual int GeoId {
151 get { return regionId; }
154 public virtual bool IsMetric {
166 public virtual string ISOCurrencySymbol {
167 get { return isoCurrencySymbol; }
171 public virtual string NativeName {
172 get { return nativeName; }
176 public virtual string CurrencyNativeName {
177 get { return currencyNativeName; }
180 public virtual string Name {
181 get { return iso2Name; }
184 public virtual string ThreeLetterISORegionName {
185 get { return iso3Name; }
188 public virtual string ThreeLetterWindowsRegionName {
189 get { return win3Name; }
192 public virtual string TwoLetterISORegionName {
193 get { return iso2Name; }
196 public override bool Equals (object value)
198 RegionInfo other = value as RegionInfo;
199 return other != null && Name == other.Name;
202 public override int GetHashCode ()
204 return Name.GetHashCode ();
207 public override string ToString ()
212 internal static void ClearCachedData ()
214 currentRegion = null;