It was a very simple thing to hear at first glance, but it was a bit of a surprise to get up.
First of all, of course, the GetType () method reflects its type information and then parses it, but the type information is not simply given a property to judge.
The foreigner gives the method is:
Public Static BOOLIsNumeric ( ThisType DataType) { if(DataType = =NULL) Throw NewArgumentNullException ("DataType"); return(DataType = =typeof(int) || DataType = =typeof(Double) || DataType = =typeof(Long) || DataType = =typeof( Short) || DataType = =typeof(float) || DataType = =typeof(Int16)|| DataType = =typeof(Int32)|| DataType = =typeof(Int64)|| DataType = =typeof(UINT) || DataType = =typeof(UInt16)|| DataType = =typeof(UInt32)|| DataType = =typeof(UInt64)|| DataType = =typeof(sbyte) || DataType = =typeof(single)); }
I'm going to take a walk. He's trying to be poor. Compare to all known numeric types .... This should be possible, that is, performance is almost and indecent.
And he seems to have forgotten the decimal ...
I looked at these numeric types, which seem to be structs rather than classes, and all have common interfaces:
IFormattable, IComparable, iconvertible
Where the IFormattable interface is an interface where the numeric type differs from several other underlying types.
This is very good, the code is as follows:
Public Static BOOL Isnumerictype ( this Type o) { returntypeof(IFormattable)); }
In addition to the basic type there are also nullable type Nullable<t>, which is commonly used for example double? This, for generic type matching I do not know what to do, in a hurry did not delve into, with a lazy way to achieve:
Public Static BOOL Isnullablenumerictype ( this Type o) { if (!o.name.startswith (" Nullable"returnfalse; return o.getgenericarguments () [0]. Isnumerictype (); }
See, just to determine the type name is not to "Nullable" start, if it is the first generic parameter type of the above judgment, so certainly not 100% reliable, I hope that insight can improve this method and share it out ha.
. NET determines whether an object is a numeric type