首页 > 解决方案 > 如何在 C# 中找出数字类型是有符号还是无符号

问题描述

我想通过反射了解有关字段类型的详细信息。

我知道我可以发现它是一个带有Type.IsValueType. 但是从那里我怎么知道它是一个数字?定点数?签名还是不签名??

有什么类似的Type.IsSigned吗?

标签: c#reflection

解决方案


没有那么多数字类型是无符号的,所以为什么不组成一个列表:

if (new Type[] { typeof(ushort), typeof(uint), typeof(ulong), typeof(byte) }.Contains(type))
{
    // unsigned.
}

或者,如果您只想比较值(此处o):

if (o is ushort || o is uint || o is ulong || o is byte)
{
    // unsigned.
}

推荐阅读