The Labs \ Source Viewer \ SSCLI \ System.Globalization \ RegionInfo

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. ////////////////////////////////////////////////////////////////////////////
  16. //
  17. // Class: RegionInfo
  18. //
  19. // Purpose: This class represents settings specified by de jure or
  20. // de facto standards for a particular country or region. In
  21. // contrast to CultureInfo, the RegionInfo does not represent
  22. // preferences of the user and does not depend on the user's
  23. // language or culture.
  24. //
  25. // Date: March 31, 1999
  26. //
  27. ////////////////////////////////////////////////////////////////////////////
  28. namespace System.Globalization
  29. {
  30.    
  31.     using System;
  32.     using System.Runtime.Serialization;
  33.    
  34.     [Serializable()]
  35.     [System.Runtime.InteropServices.ComVisible(true)]
  36.     public class RegionInfo
  37.     {
  38.         //--------------------------------------------------------------------//
  39.         // Internal Information //
  40.         //--------------------------------------------------------------------//
  41.        
  42.         //
  43.         // Variables.
  44.         //
  45.         internal string m_name;
  46.        
  47.         //
  48.         // m_cultureId only used for serialization to detect if we have created the Region using LCID.
  49.         // When creating region using culture Id we fake the name (for compatibility) so during the
  50.         // deserialization we need to detect if we have faked name or real name.
  51.         //
  52.         [OptionalField(VersionAdded = 2)]
  53.         int m_cultureId;
  54.        
  55.         //
  56.         // The CultureTable instance that we are going to read data from.
  57.         // For supported culture, this will be the CultureTable instance that read data from mscorlib assembly.
  58.         // For customized culture, this will be the CultureTable instance that read data from user customized culture binary file.
  59.         //
  60.         [NonSerialized()]
  61.         internal CultureTableRecord m_cultureTableRecord;
  62.        
  63.         static internal RegionInfo m_currentRegionInfo;
  64.        
  65.        
  66.         ////////////////////////////////////////////////////////////////////////
  67.         //
  68.         // RegionInfo Constructors
  69.         //
  70.         ////////////////////////////////////////////////////////////////////////
  71.        
  72.        
  73.         public RegionInfo(string name)
  74.         {
  75.             if (name == null)
  76.                 throw new ArgumentNullException("name");
  77.             if (name.Length == 0)
  78.                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidRegionName", name), "name");
  79.             this.m_name = name.ToUpper(CultureInfo.InvariantCulture);
  80.            
  81.             m_cultureId = 0;
  82.            
  83.             //
  84.             // First try it as an entire culture. We must have user override as true here so
  85.             // that we can pick up custom cultures *before* built-in ones (if they want to
  86.             // prefer built-in cultures they will pass "us" instead of "en-US").
  87.             //
  88.             this.m_cultureTableRecord = CultureTableRecord.GetCultureTableRecordForRegion(name, true);
  89.            
  90.             if (this.m_cultureTableRecord.IsNeutralCulture) {
  91.                 // Not supposed to be neutral
  92.                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidNeutralRegionName", name), "name");
  93.             }
  94.         }
  95.        
  96.        
  97.         public RegionInfo(int culture)
  98.         {
  99.             // Get the culture data item.
  100.            
  101.             if (culture == CultureInfo.LOCALE_INVARIANT) {
  102.                 //The InvariantCulture has no matching region
  103.                 throw new ArgumentException(Environment.GetResourceString("Argument_NoRegionInvariantCulture"));
  104.             }
  105.            
  106.             if (CultureTableRecord.IsCustomCultureId(culture)) {
  107.                 // Customized culture
  108.                 throw new ArgumentException(Environment.GetResourceString("Argument_CustomCultureCannotBePassedByNumber", "culture"));
  109.             }
  110.             if (CultureInfo.GetSubLangID(culture) == 0) {
  111.                 // Supposed to have sublang (region) info
  112.                 throw new ArgumentException(Environment.GetResourceString("Argument_CultureIsNeutral", culture), "culture");
  113.             }
  114.            
  115.             //
  116.             // From this region data item, get the region data item.
  117.             // Several culture ID may map to the same region.
  118.             // For example, 0x1009 (English (Canada)) and 0x0c0c (French (Canada)) all map to
  119.             // the same region "CA" (Canada).
  120.             //
  121.             this.m_cultureTableRecord = CultureTableRecord.GetCultureTableRecord(culture, true);
  122.            
  123.             if (this.m_cultureTableRecord.IsNeutralCulture) {
  124.                 // Not supposed to be neutral
  125.                 throw new ArgumentException(Environment.GetResourceString("Argument_CultureIsNeutral", culture), "culture");
  126.             }
  127.            
  128.             // Make sure that we initialize m_name since it will be used in deserialization.
  129.             this.m_name = this.m_cultureTableRecord.SREGIONNAME;
  130.            
  131.             m_cultureId = culture;
  132.         }
  133.        
  134.         internal RegionInfo(CultureTableRecord table)
  135.         {
  136.             this.m_cultureTableRecord = table;
  137.             // Make sure that we initialize m_name since it will be used in deserialization.
  138.             this.m_name = this.m_cultureTableRecord.SREGIONNAME;
  139.         }
  140.        
  141.         #region Serialization
  142.         internal int m_dataItem;
  143.        
  144.         [OnDeserialized()]
  145.         private void OnDeserialized(StreamingContext ctx)
  146.         {
  147.             if (m_name == null) {
  148.                 BCLDebug.Assert(m_dataItem >= 0, "[RegionInfo.OnDeserialized] null name and invalid dataItem");
  149.                 m_cultureTableRecord = CultureTableRecord.GetCultureTableRecord(CultureTableRecord.IdFromEverettRegionInfoDataItem(m_dataItem), true);
  150.                 m_name = m_cultureTableRecord.SREGIONNAME;
  151.             }
  152.             else {
  153.                 if (m_cultureId != 0)
  154.                     m_cultureTableRecord = CultureTableRecord.GetCultureTableRecord(m_cultureId, true);
  155.                 else
  156.                     m_cultureTableRecord = CultureTableRecord.GetCultureTableRecordForRegion(m_name, true);
  157.             }
  158.         }
  159.        
  160.         [OnSerializing()]
  161.         private void OnSerializing(StreamingContext ctx)
  162.         {
  163.             m_dataItem = m_cultureTableRecord.EverettRegionDataItem();
  164.         }
  165.         #endregion Serialization
  166.        
  167.         ////////////////////////////////////////////////////////////////////////
  168.         //
  169.         // GetCurrentRegion
  170.         //
  171.         // This instance provides methods based on the current user settings.
  172.         // These settings are volatile and may change over the lifetime of the
  173.         // thread.
  174.         //
  175.         ////////////////////////////////////////////////////////////////////////
  176.        
  177.        
  178.         unsafe public static RegionInfo CurrentRegion {
  179.             get {
  180.                 RegionInfo temp = m_currentRegionInfo;
  181.                 if (temp == null) {
  182.                     temp = new RegionInfo(CultureInfo.CurrentCulture.m_cultureTableRecord);
  183.                    
  184.                     // Need full name for custom cultures
  185.                     if (temp.m_cultureTableRecord.IsCustomCulture) {
  186.                         temp.m_name = temp.m_cultureTableRecord.SNAME;
  187.                     }
  188.                     m_currentRegionInfo = temp;
  189.                 }
  190.                 return temp;
  191.             }
  192.         }
  193.        
  194.         ////////////////////////////////////////////////////////////////////////
  195.         //
  196.         // GetName
  197.         //
  198.         // Returns the name of the region in the UI language.
  199.         //
  200.         ////////////////////////////////////////////////////////////////////////
  201.        
  202.        
  203.         public virtual string Name {
  204.             get {
  205.                 if (m_name == null) {
  206.                     m_name = this.m_cultureTableRecord.SREGIONNAME;
  207.                 }
  208.                 return (m_name);
  209.             }
  210.         }
  211.        
  212.         ////////////////////////////////////////////////////////////////////////
  213.         //
  214.         // GetEnglishName
  215.         //
  216.         // Returns the name of the region in English.
  217.         //
  218.         ////////////////////////////////////////////////////////////////////////
  219.        
  220.        
  221.         public virtual string EnglishName {
  222.             get { return (this.m_cultureTableRecord.SENGCOUNTRY); }
  223.         }
  224.        
  225.        
  226.         public virtual string DisplayName {
  227.             get {
  228.                 if (m_cultureTableRecord.IsCustomCulture) {
  229.                     if (m_cultureTableRecord.IsReplacementCulture) {
  230.                         // <SyntheticSupport/>
  231.                         if (m_cultureTableRecord.IsSynthetic)
  232.                             return m_cultureTableRecord.RegionNativeDisplayName;
  233.                         else
  234.                             return (Environment.GetResourceString("Globalization.ri_" + this.m_cultureTableRecord.SREGIONNAME));
  235.                     }
  236.                     else {
  237.                         return this.m_cultureTableRecord.SNATIVECOUNTRY;
  238.                     }
  239.                 }
  240.                 else {
  241.                     // <SyntheticSupport/>
  242.                     if (m_cultureTableRecord.IsSynthetic)
  243.                         return m_cultureTableRecord.RegionNativeDisplayName;
  244.                     else
  245.                         return (Environment.GetResourceString("Globalization.ri_" + this.m_cultureTableRecord.SREGIONNAME));
  246.                 }
  247.             }
  248.         }
  249.        
  250.        
  251.         [System.Runtime.InteropServices.ComVisible(false)]
  252.         public virtual string NativeName {
  253.             get { return (this.m_cultureTableRecord.SNATIVECOUNTRY); }
  254.         }
  255.        
  256.        
  257.         public virtual string TwoLetterISORegionName {
  258.             get { return (this.m_cultureTableRecord.SISO3166CTRYNAME); }
  259.         }
  260.        
  261.        
  262.         public virtual string ThreeLetterISORegionName {
  263.             get { return (this.m_cultureTableRecord.SISO3166CTRYNAME2); }
  264.         }
  265.        
  266.        
  267.         public virtual bool IsMetric {
  268.             get {
  269.                 int value = this.m_cultureTableRecord.IMEASURE;
  270.                 return (value == 0);
  271.             }
  272.         }
  273.        
  274.        
  275.         [System.Runtime.InteropServices.ComVisible(false)]
  276.         public virtual int GeoId {
  277.             get { return (this.m_cultureTableRecord.IGEOID); }
  278.         }
  279.        
  280.        
  281.         public virtual string ThreeLetterWindowsRegionName {
  282.             get { return (this.m_cultureTableRecord.SABBREVCTRYNAME); }
  283.         }
  284.        
  285.        
  286.         [System.Runtime.InteropServices.ComVisible(false)]
  287.         public virtual string CurrencyEnglishName {
  288.             get { return (this.m_cultureTableRecord.SENGLISHCURRENCY); }
  289.         }
  290.        
  291.        
  292.         [System.Runtime.InteropServices.ComVisible(false)]
  293.         public virtual string CurrencyNativeName {
  294.             get { return (this.m_cultureTableRecord.SNATIVECURRENCY); }
  295.         }
  296.        
  297. /*================================ CurrencySymbol =============================
  298.         **Property: CurrencySymbol
  299.         **Exceptions: None
  300.         ==============================================================================*/       
  301.        
  302.        
  303.         public virtual string CurrencySymbol {
  304.             get { return (this.m_cultureTableRecord.SCURRENCY); }
  305.         }
  306.        
  307. /*================================ ISOCurrencySymbol ==========================
  308.         **Property: Three characters of the international monetary symbol specified in ISO 4217.
  309.         **Exceptions: None
  310.         ==============================================================================*/       
  311.        
  312.        
  313.         public virtual string ISOCurrencySymbol {
  314.             get { return (this.m_cultureTableRecord.SINTLSYMBOL); }
  315.         }
  316.        
  317.         ////////////////////////////////////////////////////////////////////////
  318.         //
  319.         // Equals
  320.         //
  321.         // Implements Object.Equals(). Returns a boolean indicating whether
  322.         // or not object refers to the same CultureInfo as the current instance.
  323.         //
  324.         ////////////////////////////////////////////////////////////////////////
  325.        
  326.        
  327.         public override bool Equals(object value)
  328.         {
  329.             RegionInfo that = value as RegionInfo;
  330.             if (that != null) {
  331.                 return this.Name.Equals(that.Name);
  332.             }
  333.            
  334.             return (false);
  335.         }
  336.        
  337.         ////////////////////////////////////////////////////////////////////////
  338.         //
  339.         // GetHashCode
  340.         //
  341.         // Implements Object.GetHashCode(). Returns the hash code for the
  342.         // CultureInfo. The hash code is guaranteed to be the same for RegionInfo
  343.         // A and B where A.Equals(B) is true.
  344.         //
  345.         ////////////////////////////////////////////////////////////////////////
  346.        
  347.        
  348.         public override int GetHashCode()
  349.         {
  350.             return (this.Name.GetHashCode());
  351.         }
  352.        
  353.        
  354.         ////////////////////////////////////////////////////////////////////////
  355.         //
  356.         // ToString
  357.         //
  358.         // Implements Object.ToString(). Returns the name of the Region,
  359.         // eg. "United States".
  360.         //
  361.         ////////////////////////////////////////////////////////////////////////
  362.        
  363.        
  364.         public override string ToString()
  365.         {
  366.             return (Name);
  367.         }
  368.     }
  369. }

Developer Fusion