当前位置: 首页>>代码示例>>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;未经允许,请勿转载。