public static bool IsNumericType(this Type o)
{
return !o.IsClass && !o.IsInterface && o.GetInterfaces().Any(q => q == typeof(IFormattable));
}
另外除了基本类型之外还有可空类型Nullable<T>,就是常用的例如double?这种,对于泛型的类型的匹配我不知该怎么做才好,赶时间就没深究,用了个偷懒的方法实现了:
public static bool IsNullableNumericType(this Type o)
{
if (!o.Name.StartsWith("Nullable")) return false;
return o.GetGenericArguments()[0].IsNumericType();
}
看吧,只是判断一下类型名称是不是以“Nullable”开始,如果是的话再对其第一个泛型参数类型进行上面的判断,这样肯定不是100%靠谱的,感兴趣的朋友可以进一步完善一下这个方法。
希望本文所述对大家的.net程序设计有所帮助。








