. Net is used to determine whether an object is a numeric instance. net Value

Source: Internet
Author: User

. Net is used to determine whether an object is a numeric instance. net Value

This example describes how to use. Net to determine whether an object is of the numerical type. The specific implementation method is as follows:

. Net is very easy to judge whether an object is of the numerical type at first glance, but it suddenly seems a little 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:

Copy codeThe Code is as follows: 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)
);
}

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...

We further studied these numerical types, which seem to be both structured rather than class, and all have the same interface:

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

The Code is as follows:

Copy codeThe 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:

Copy codeThe Code is as follows: 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, if you are interested, you can further improve this method.

I hope this article will help you with the. net program design.


16. In NET, a page object is an instance of the () class.

System. Web. UI. Page

Net, how does one determine whether a certain instance has a property named "XXX? Example

Try the following method:
Private void button3_Click (object sender, EventArgs e)
{
Student xiaofu = new Student ();
Xiaofu. Name = "frj ";
Xiaofu. ID = "001 ";
Bool isCotains = IsContainsProperty (xiaofu, "Name ");
MessageBox. Show (isCotains. ToString ());

}

Public bool IsContainsProperty (Student sInfo, string propertyName)
{
// Obtain all attributes in C # (xiaofu is a custom object)
System. Reflection. PropertyInfo [] pinfo = sInfo. GetType (). GetProperties ();
// Loop all attribute Fields
Foreach (System. Reflection. PropertyInfo p in pinfo)
{
If (p. Name = propertyName)
{
Return true;
}
}
Return false;
}
Public class Student
{
String _ name = "";
String _ id = "";
Public string Name
{
Get {return _ name ;}
Set {_ name = value ;}
}
Public string ID
{
Get {return _ id ;}
Set {_ id = value ;}
}
}

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.