首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> ASP.NET 基礎

.NET 判斷類型是否為數值類型:從枚舉法到接口特徵法的演進

     編輯:ASP.NET 基礎

問題背景

在反射、序列化、模型綁定、動態代碼生成等場景中,經常需要判斷 `Type` 對象是否代表數值類型 (int, double, decimal 等)。.NET Framework **未提供** `Type.IsNumeric` 內置屬性,開發者需自行實現。

方案一:枚舉法 (朴素但完整)

public static bool IsNumericInterface(this Type type)
{
    if (type == null) throw new ArgumentNullException(nameof(type));

    // 1. 必須是值類型 (struct) 且非 enum
    if (!type.IsValueType || type.IsEnum) return false;

    // 2. 解包 Nullable
    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
        type = type.GetGenericArguments()[0];

    // 3. 核心判斷:實現 IFormattable 且非 Char/Boolean/DateTime/Guid
    // 注意:Char 也實現 IFormattable,需排除
    if (type == typeof(char) || type == typeof(bool) || type == typeof(DateTime) || type == typeof(Guid))
        return false;

    return typeof(IFormattable).IsAssignableFrom(type);
}

方案三:.NET 5+ 型別特性 API (現代、高性能)

// BenchmarkDotNet 結果 (示意)
| Method            | Mean (ns) | Allocated |
|-------------------|----------:|----------:|
| IsNumericEnum     |    12.3   |    0 B    |
| IsNumericInterface|    45.7   |   24 B    | (反射 GetInterfaces 分配數組)
| IsNumericModern   |     3.1   |    0 B    | (TypeCode 查表最快)

完整生產級實現


應用場景示例


總結建議

  1. 現代項目 (.NET Core 3.1+ / .NET 5+):優先使用 TypeCode 方案 (`IsNumericModern`),性能最佳、代碼最少、語義清晰。
  2. 舊項目 (.NET Framework 4.x):`Type.GetTypeCode` 同樣可用,推薦作為首選。
  3. 需支持自定義數值類型:結合接口特徵法 (`IFormattable` + 排除列表) 或 .NET 7+ `INumber` 接口。
  4. 避免:`GetInterfaces().Any()` 反射分配數組,高頻調用有 GC 壓力。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved