C# - how to determine whether a Type is a number

后端 未结 18 2297
傲寒
傲寒 2020-11-28 22:47

Is there a way to determine whether or not a given .Net Type is a number? For example: System.UInt32/UInt16/Double are all numbers. I want to avoid a long switc

18条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 23:48

    Try the TypeSupport nuget package for C#. It has support for detecting all numeric types (among many other features):

    var extendedType = typeof(int).GetExtendedType();
    Assert.IsTrue(extendedType.IsNumericType);
    

提交回复
热议问题