XiuXian.China.Nations 1.0.2

dotnet add package XiuXian.China.Nations --version 1.0.2
                    
NuGet\Install-Package XiuXian.China.Nations -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XiuXian.China.Nations" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XiuXian.China.Nations" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="XiuXian.China.Nations" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XiuXian.China.Nations --version 1.0.2
                    
#r "nuget: XiuXian.China.Nations, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package XiuXian.China.Nations@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XiuXian.China.Nations&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=XiuXian.China.Nations&version=1.0.2
                    
Install as a Cake Tool

XiuXian.China.Nations 🇨🇳

基于国标(GB/T 2260)的 56 民族数据处理库 — 零依赖、线程安全、开箱即用。

.NET Standard NuGet License Gitee


开源地址


功能特性

特性 说明
🗂️ 完整数据 56个民族国标编码(01-56)+ "其他民族"(99)
📌 多维信息 标准名称、拼音标识、短码、枚举值,一应俱全
🔄 双向转换 枚举 ↔ 国标编码 ↔ 实体,无缝互转
🔍 灵活查询 支持编码 / 名称 / 拼音 / 短码 / 枚举,5种查询维度
🧠 智能检索 名称、拼音、短码多维度模糊搜索(大小写不敏感)
📊 排序支持 按拼音字母排序
🧵 线程安全 静态类 + Lazy<T> 延迟初始化,开箱即用
📦 零依赖 纯 .NET Standard API,无第三方包
⚡ 高性能 3 个 Dictionary + 1 个 ILookup 索引,查询 O(1)
🔒 防御性拷贝 返回不可变视图,防止外部篡改

支持框架

  • .NET Standard 2.0+(兼容 .NET Framework 4.6.1+ / .NET Core 2.0+ / .NET 5+ / .NET 6+ / .NET 8+ / .NET 10+)
  • Mono / Xamarin / Unity

快速开始

安装

dotnet add package XiuXian.China.Nations

或者通过 NuGet 包管理器:

Install-Package XiuXian.China.Nations

一分钟上手

using XiuXian.China.Nations.Services;
using XiuXian.China.Nations.Enums;
using XiuXian.China.Nations.Extensions;

// 1. 获取所有民族
var all = NationLibrary.GetAll();
Console.WriteLine($"共 {all.Count} 个民族");

// 2. 按编码查询(支持 "1" 或 "01")
var han = NationLibrary.GetByCode("01");      // 汉族
var menggu = NationLibrary.GetByCode("02");   // 蒙古族

// 3. 按名称查询
var hui = NationLibrary.GetByName("回族");

// 4. 按枚举查询
var zang = NationLibrary.GetByEnum(NationEnum.Zang);

// 5. 模糊搜索(名称/拼音/短码)
var search1 = NationLibrary.Search("蒙");      // 按名称
var search2 = NationLibrary.Search("Meng");    // 按拼音
var search3 = NationLibrary.Search("MGZ");     // 按短码

项目结构

XiuXian.China.Nations/
├── Enums/
│   └── NationEnum.cs          # 56个民族枚举定义
├── Models/
│   └── NationInfo.cs          # 民族信息实体模型
├── Services/
│   └── NationLibrary.cs       # 民族数据核心服务(静态类)
├── Extensions/
│   └── NationExtensions.cs    # 民族扩展方法
├── Utils/
│   └── CodeNormalizer.cs      # 国标编码标准化工具
├── XiuXian.China.Nations.csproj
└── README.md

使用指南

1. 数据查询

// 获取全部列表
IReadOnlyList<NationInfo> all = NationLibrary.GetAll();

// 精确查询(返回 null 表示未找到)
NationInfo? nation = NationLibrary.GetByCode("56");        // 编码
NationInfo? nation = NationLibrary.GetByName("基诺族");     // 名称
NationInfo? nation = NationLibrary.GetByEnum(NationEnum.JiNuo);  // 枚举

// 短码查询(可能返回多条)
IReadOnlyList<NationInfo> results = NationLibrary.GetByShortCode("HZ");

2. 枚举 ↔ 编码 双向转换

// 枚举 → 国标编码
string code = NationLibrary.GetCodeByEnum(NationEnum.Han);   // "01"

// 国标编码 → 枚举
NationEnum en = NationLibrary.GetEnumByCode("02");           // NationEnum.MengGu

3. 模糊搜索

支持按名称、拼音、短码三个维度同时模糊匹配,内部使用 ToUpperInvariant() 进行大小写不敏感比较:

// 按中文名称搜索
var r1 = NationLibrary.Search("蒙古");    // → [蒙古族]

// 按拼音搜索
var r2 = NationLibrary.Search("meng");    // → [蒙古族]

// 按短码搜索
var r3 = NationLibrary.Search("MGZ");     // → [蒙古族]

// 不传参数返回全部
var all = NationLibrary.Search(null);

4. 排序与校验

// 按拼音排序
var sorted = NationLibrary.OrderByPinyin();

// 校验编码/名称合法性
bool valid = NationLibrary.IsValidCode("01");    // true
bool valid = NationLibrary.IsValidName("汉族");   // true

5. 扩展方法

using XiuXian.China.Nations.Extensions;

var nation = NationLibrary.GetByCode("01");

bool isHan      = nation.IsHan();         // true — 判断汉族(null → false)
bool isMinority = nation.IsMinority();    // false — 判断少数民族(null/其他民族 → false)
string shortName = nation.GetShortName(); // "汉" — 简称(取首字/首两字)

// 批量查询
var batch = new[] { "01", "02", "03" }.GetNationsByCodes();
var batch = new[] { "汉族", "回族" }.GetNationsByNames();

扩展方法说明:

方法 扩展类型 返回值 说明
IsHan() NationInfo?(可空) bool 是否为汉族;null 返回 false
IsMinority() NationInfo?(可空) bool 是否为少数民族(非汉族且非"其他民族");null 返回 false
GetShortName() NationInfo(不可空) string 名称长度>2取前两字(蒙古族→"蒙古"),否则返回原名(土族→"土族");名称为空返回空字符串
GetNationsByCodes() IEnumerable<string> List<NationInfo?> 批量按编码查询
GetNationsByNames() IEnumerable<string> List<NationInfo?> 批量按名称查询

6. 短码歧义检测

// 检查某个短码是否有歧义
bool ambiguous = NationLibrary.IsAmbiguousShortCode("HZ"); // true(汉/回)

// 获取所有重复的短码
var duplicates = NationLibrary.GetDuplicateShortCodes();
// → { "HZ": [汉族, 回族], "ZZ": [藏族, 壮族], ... }

数据模型

NationInfo 实体

[Serializable]
public class NationInfo
{
    public string    Code       { get; set; }  // 国标编码 "01"~"56", "99"(默认 string.Empty)
    public string    Name       { get; set; }  // 中文名称(如"蒙古族",默认 string.Empty)
    public string    Pinyin     { get; set; }  // 拼音标识(如"MengGu",默认 string.Empty)
    public string    ShortCode  { get; set; }  // 短码(拼音首字母+Z,默认 string.Empty)
    public NationEnum EnumValue { get; set; }  // 枚举值
}

重写方法:

方法 说明
ToString() 返回 Name
Equals(object?) 基于 Code 做 Ordinal 比较,跨平台一致
GetHashCode() 基于 Code 的 Ordinal 哈希码

⚠️ GetHashCode() 基于可变属性 Code,放入 Dictionary/HashSet 后请勿修改 Code。

NationEnum 枚举

成员 值 说明
None 0 未指定(默认值)
Han 1 汉族
MengGu 2 蒙古族
... ... 56个民族按国标编号 01-56
JiNuo 56 基诺族
Other 99 其他/未识别民族

枚举名称使用驼峰拼音(如 MengGu、WeiWuEr、KeErKeZi)。

ShortCode 规则

取各字拼音首字母 + Z(族) 后缀:

名称 拼音 ShortCode 说明
汉族 Han HZ H(汉) + Z(族)
蒙古族 MengGu MGZ M(蒙) + G(古) + Z(族)
维吾尔族 WeiWuEr WWEZ W(维) + W(吾) + E(尔) + Z(族)

已知短码重复

以下短码对应多个民族,GetByShortCode() 返回列表而非单条:

短码 对应民族
HZ 汉族(01)、回族(03)
ZZ 藏族(04)、壮族(08)
MZ 苗族(06)、满族(11)
YZ 彝族(07)、瑶族(13)
DZ 侗族(12)、傣族(18)
SZ 畲族(22)、水族(25)

API 参考

NationLibrary(核心服务)

内部索引结构:

索引 类型 键 说明
_codeMap Dictionary<string, NationInfo> 国标编码 支持 "1"/"01" 两种格式
_nameMap Dictionary<string, NationInfo> 中文名称 精确匹配
_enumMap Dictionary<NationEnum, NationInfo> 枚举值 枚举 → 实体
_shortCodeLookup ILookup<string, NationInfo> 短码 一对多(短码可能重复)

所有索引通过 Lazy<T> 实现线程安全的延迟初始化。

完整 API 列表:

方法 参数 返回值 说明
GetAll() - IReadOnlyList<NationInfo> 获取全部民族(防御性拷贝)
GetByCode(code) string? NationInfo? 按国标编码查询("1"/"01"均可)
GetByName(name) string? NationInfo? 按中文名称精准查询
GetByShortCode(shortCode) string? IReadOnlyList<NationInfo> 按短码查询(可能重复)
GetByEnum(nation) NationEnum NationInfo? 按枚举值查询
GetCodeByEnum(nation) NationEnum string 枚举 → 国标编码
GetEnumByCode(code) string? NationEnum 编码 → 枚举(无效返回 NationEnum.None)
Search(keyword) string? IReadOnlyList<NationInfo> 多维度模糊搜索(名称/拼音/短码)
OrderByPinyin() - IReadOnlyList<NationInfo> 按拼音字母排序
IsValidCode(code) string? bool 校验国标编码(01-56, 99)
IsValidName(name) string? bool 校验民族名称合法性
IsAmbiguousShortCode(shortCode) string bool 短码是否有歧义(对应多个民族)
GetDuplicateShortCodes() - IReadOnlyDictionary<string, List<NationInfo>> 获取所有重复短码

NationExtensions(扩展方法)

方法 扩展类型 返回值 说明
IsHan() NationInfo? bool 是否为汉族;null 返回 false
IsMinority() NationInfo? bool 是否为少数民族;null 或"其他民族"返回 false
GetShortName() NationInfo string 取名称首字/首两字
GetNationsByCodes() IEnumerable<string> List<NationInfo?> 批量按编码查询
GetNationsByNames() IEnumerable<string> List<NationInfo?> 批量按名称查询

CodeNormalizer(编码标准化工具)

位于 XiuXian.China.Nations.Utils 命名空间,被 NationLibrary 内部调用,也可单独使用。

方法 参数 返回值 说明
NormalizeCode(code) string? string 标准化编码:"1" → "01",保留 "01" 不变;无效/空白返回空字符串
IsValidNumericCode(code) string bool 校验是否为有效民族编码(01-56 或 99)

NormalizeCode 规则:

  • 空白/空输入 → 返回 string.Empty
  • 1 位纯数字 → 左侧补零
  • 2 位纯数字 → 直接返回
  • 非纯数字输入 → 原样返回,由调用方处理

注意事项

  • 查询安全:所有查询方法返回 null 或空列表,不会抛出异常
  • 编码容错:支持 "1" 和 "01" 两种编码格式(通过 CodeNormalizer.NormalizeCode() 自动标准化)
  • 短码歧义:HZ/ZZ/MZ/YZ/DZ/SZ 有重复,使用 GetByShortCode() 获取列表
  • HashCode 不可变:NationInfo 重写了 Equals()/GetHashCode()(基于 Code 的 Ordinal 比较),放入 Dictionary/HashSet 后不要修改 Code
  • 序列化:NationInfo 标记了 [Serializable],适用于 JSON/数据库序列化
  • IsMinority 边界:"其他民族"(Code="99")既不是汉族,也不计入少数民族

版本历史

版本 说明
v1.0.2 更新文档,添加 LICENSE(MIT)、公众号二维码;补充 CodeNormalizer、NationInfo 序列化等 API 说明;优化文档结构与可读性
v1.0.1 修正 ShortCode 数据错误(蒙古族 MGG→MGZ、土家族 TJJ→TJZ、柯尔克孜族 KKEKZ→KEKZ);统一拼音命名
v1.0.0 初始发布:完整56民族数据、多维查询、枚举转换、模糊搜索、拼音排序


关注我们

公众号:.NET修仙日记

定期分享 .NET 开发实战、开源项目解读与技术干货。

.NET修仙日记


许可证

MIT License — 详见 LICENSE 文件。


XiuXian.China.Nations — 让民族数据处理更简单 🇨🇳

开源地址:https://gitee.com/yzxs949/xiu-xian/tree/master/XiuXian.China.Nations

公众号:.NET修仙日记

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 141 6/26/2026
1.0.1 125 5/24/2026
1.0.0 117 5/24/2026
1.0.0-alpha 117 5/22/2026

v1.0.2
     - 更新文档,添加 LICENSE(MIT)、公众号二维码
     - 补充 CodeNormalizer、NationInfo 序列化等 API 说明
     - 优化文档结构与可读性