. Net determines whether an object is of the numerical type. net object Value Type

Source: Internet
Author: User

. Net determines whether an object is of the numerical type. net object Value Type

At first glance, it was a very simple task, but suddenly it was a bit difficult to start.

First of all, we get the Type information through the GetType () method reflection and analyze it. However, the Type information Type does not simply give such an attribute for determination.

A foreigner provides the following methods:

public static bool IsNumeric(this Type dataType)    {        if (dataType == null)            throw new ArgumentNullException("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)               );    }

Let me go... He is trying to compare all the known numeric types .... This should be acceptable, that is, the performance is almost indecent.

And ~ He seems to have forgotten Decimal...

I have studied these numeric types. They all seem to be structures rather than classes, and they all have common interfaces:

IFormattable, IComparable, IConvertible

The IFormattable interface is a numeric interface different from several other basic types.

The Code is as follows:

public static bool IsNumericType(this Type o)    {        return !o.IsClass && !o.IsInterface && o.GetInterfaces().Any(q => q == typeof(IFormattable));    }

In addition to the basic type, there is also a possible null type Nullable <T>, which is commonly used, such as double? In this way, I don't know how to do this for generic type matching, so I didn't go into details in a hurry and implemented it in a lazy way:

public static bool IsNullableNumericType(this Type o)    {        if (!o.Name.StartsWith("Nullable")) return false;        return o.GetGenericArguments()[0].IsNumericType();    }

Check it out. It just determines whether the type name starts with "Nullable". If yes, then the above judgment will be made on the first generic parameter type. This is certainly not 100% reliable, I hope that people of insight can improve this method and share it with me.

How does net determine whether a value is a number?

// Determine the number
Public bool IsNum (string Str)
Bool bl = false;
String Rx = @ "^ [1-9] \ d * $ ";
If (Regex. IsMatch (Str, Rx ))
Bl = true;
Bl = false;
Return bl;
Thank you.

In c #, how does one determine whether the input is a character or a numerical value?

A. GetType () gets the type object of the current variable.
Typeof (String) is a String type object.
You can compare a. GetType () = typeof (String) to obtain whether a is of the String type.

Of course, there is a simpler method: a is String to obtain a boolean value to indicate whether a is of the String type or whether it can be implicitly converted to a type of the String type (of course, String cannot be a subclass, the inheritance class you write can be determined)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.