當前位置: 首頁>>代碼示例>>C#>>正文


C# SortVersion類代碼示例

本文整理匯總了C#中System.Globalization.SortVersion的典型用法代碼示例。如果您正苦於以下問題:C# SortVersion類的具體用法?C# SortVersion怎麽用?C# SortVersion使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


SortVersion類屬於System.Globalization命名空間,在下文中一共展示了SortVersion類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Region

//引入命名空間
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

public class Example : IComparer
{
   private const string FILENAME = @".\Regions.dat";

   private struct Region
   {
      internal Region(string id, string name) 
      {
         this.Id = id;
         this.NativeName = name;
      }
      
      public string Id;
      public string NativeName;
      
      public override string ToString()
      {
         return this.NativeName;
      }
   }

   public static void Main()
   {
      bool reindex = false;
      
      Region[] regions;
      SortVersion ver = null;

      // If the data has not been saved, create it.
      if (! File.Exists(FILENAME)) { 
         regions = GenerateData();
         ver = CultureInfo.CurrentCulture.CompareInfo.Version;  
         reindex = true;
      }
      // Retrieve the existing data.
      else {
         regions = RestoreData(out ver);
      }

      // Determine whether the current ordering is valid; if not, reorder.
      if (reindex || ver != CultureInfo.CurrentCulture.CompareInfo.Version) { 
         Array.Sort(regions, new Example());      
         // Save newly reordered data.
         SaveData(regions);
      }
      
      // Continue with application...
   }

   private static Region[] GenerateData()
   {
      List<Region> regions = new List<Region>();

      foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures)) {
         if (culture.IsNeutralCulture | culture.Equals(CultureInfo.InvariantCulture))
            continue;
            
         RegionInfo region = new RegionInfo(culture.Name);
         regions.Add(new Region(region.Name, region.NativeName));
      }
      return regions.ToArray();
   }

   private static Region[] RestoreData(out SortVersion ver)
   {
      List<Region> regions = new List<Region>();
      
      BinaryReader rdr = new BinaryReader(File.Open(FILENAME, FileMode.Open));
      
      int sortVer = rdr.ReadInt32();
      Guid sortId = Guid.Parse(rdr.ReadString());
      ver = new SortVersion(sortVer, sortId);
      
      string id, name;
      while (rdr.PeekChar() != -1) {
         id = rdr.ReadString();
         name = rdr.ReadString();
         regions.Add(new Region(id, name));      
      }
      return regions.ToArray();
   }

   private static void SaveData(Region[] regions)
   {
      SortVersion ver = CultureInfo.CurrentCulture.CompareInfo.Version;

      BinaryWriter wrtr = new BinaryWriter(File.Open(FILENAME, FileMode.Create));
      wrtr.Write(ver.FullVersion); 
      wrtr.Write(ver.SortId.ToString()); 
      
      foreach (var region in regions) {
         wrtr.Write(region.Id);
         wrtr.Write(region.NativeName);
      }
      wrtr.Close();
   }

   public int Compare(object o1, object o2)
   {
        // Assume that all casts succeed.
        Region r1 = (Region) o1;
        Region r2 = (Region) o2;
        
        return String.Compare(r1.NativeName, r2.NativeName, 
                              StringComparison.CurrentCulture);        
   }
}
開發者ID:.NET開發者,項目名稱:System.Globalization,代碼行數:115,代碼來源:SortVersion


注:本文中的System.Globalization.SortVersion類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。