c# - 如何在 C# 中找出数字类型是有符号还是无符号
问题描述
我想通过反射了解有关字段类型的详细信息。
我知道我可以发现它是一个带有Type.IsValueType
. 但是从那里我怎么知道它是一个数字?定点数?签名还是不签名??
有什么类似的Type.IsSigned
吗?
解决方案
没有那么多数字类型是无符号的,所以为什么不组成一个列表:
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.
}
推荐阅读
- android - 当相机访问被阻止时生物识别提示(面部认证)(Android 12 - Pixel)
- c++ - 来自 fstream 的 C++ 2d 数组未正确存储
- r - SEM 中的回归与提取的潜在因素的回归不匹配
- mysql - 选择语句不适用于可能保留字的列名
- python - 如何将 MultiIndex 行附加到空的熊猫数据框
- r - 如何使用多个旧列的值创建新列?
- api - GET 请求来自 Postman,但不适用于 Android 和浏览器
- azure-cosmosdb - 如何从创建时获得 Azure Cosmos DB 项 TTL?
- r - 取R中列组的第一个值
- c++ - 共享指针管理堆栈对象