You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GetProperty()获取C#数值类型Min/Max值返回null的原因

问题成因分析:C#内置数值类型MinValue/MaxValue获取返回null

尝试编写代码输出C#各类数值类型(如int、uint等)的详细信息,包括MinValue、MaxValue、sizeof以及类型名称,但运行后发现通过type.GetProperty("MinValue")?.GetValue(instance)获取内置数值类型的MinValue和MaxValue时返回null。代码如下:

Console.WriteLine(new string('-', 120));
Console.WriteLine("{0,-10} {1,-20} {2,40} {3,40}",
    "Type", "Byte(s) of memory", "Min", "Max");
Console.WriteLine(new string('-', 120));
Type[] types = new Type[] {
    typeof(byte),
    typeof(sbyte),
    typeof(int),
    typeof(uint),
    typeof(short),
    typeof(ushort),
    typeof(long),
    typeof(ulong),
    typeof(Int128),
    typeof(UInt128),
    typeof(Half),
    typeof(float),
    typeof(double),
    typeof(decimal),
};

int[] typesSizes = new int[]
{
    sizeof(System.Byte),
    sizeof(sbyte),
    sizeof(int),
    sizeof(uint),
    sizeof(short),
    sizeof(ushort),
    sizeof(long),
    sizeof(ulong),
    sizeof(Int128),
    sizeof(UInt128),
    sizeof(Half),
    sizeof(float),
    sizeof(double),
    sizeof(decimal),
};

dynamic instance;
int size;
Type type;

for (int i = 0; i < typesSizes.Length; i++)
{
    size = typesSizes[i];
    type = types[i];

    instance = Activator.CreateInstance(type);

    if (instance == null)
    {
        throw new NullReferenceException();
    }
    
    var minValue = type.GetProperty("MinValue")?.GetValue(instance);// here it returns null for built-in types
    var maxValue = type.GetProperty("MaxValue")?.GetValue(instance);

    Console.WriteLine("{0,-10} {1,-20} {2,40} {3,40}",
    type.Name, size, minValue, maxValue);
}

核心成因

  • MinValue/MaxValue是静态属性:C#内置数值类型的MinValue和MaxValue均为public static属性,而Type.GetProperty(string name)默认仅检索实例属性,不会包含静态成员,因此直接调用会返回null。
  • GetValue参数误用:即便找到静态属性,GetValue(instance)传入实例也是多余的——静态属性属于类型本身而非实例,正确做法是传入null。
  • 实例创建完全冗余:静态属性不需要依赖实例就能访问,代码中Activator.CreateInstance(type)的步骤可以直接省略。

修正方案

修改属性检索逻辑,指定绑定标志获取静态属性,同时简化代码:

Console.WriteLine(new string('-', 120));
Console.WriteLine("{0,-10} {1,-20} {2,40} {3,40}",
    "Type", "Byte(s) of memory", "Min", "Max");
Console.WriteLine(new string('-', 120));

Type[] types = new Type[] {
    typeof(byte), typeof(sbyte), typeof(int), typeof(uint),
    typeof(short), typeof(ushort), typeof(long), typeof(ulong),
    typeof(Int128), typeof(UInt128), typeof(Half),
    typeof(float), typeof(double), typeof(decimal),
};

foreach (var type in types)
{
    // 自动获取类型大小,无需单独维护size数组
    int size = System.Runtime.InteropServices.Marshal.SizeOf(type);
    
    // 指定绑定标志检索静态公共属性
    var minProp = type.GetProperty("MinValue", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
    var maxProp = type.GetProperty("MaxValue", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
    
    // 静态属性GetValue传入null即可
    var minValue = minProp?.GetValue(null);
    var maxValue = maxProp?.GetValue(null);

    Console.WriteLine("{0,-10} {1,-20} {2,40} {3,40}",
        type.Name, size, minValue, maxValue);
}

内容的提问来源于stack exchange,提问作者Mr.Navid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 05:25:33