Some simple helper classes (1) -- STRING type verification

Source: Internet
Author: User

At work, it is often used to verify whether the value in String belongs to the Int type, Bool type, or Date type. tryParse (string, type) for verification. string strValue = "123"; int intValue; bool isInt = int. tryParse (strValue, out intValue); however, to use this method, you must create a variable of this type. what I wrote at work is that WEB programs often have to verify over 10 value types from the foreground on a page, which is a little troublesome. because we only need to determine whether the string value is of this type. I found that the TryParse method basically exists for the bool int date type, but none of their interfaces found this method. after querying the IConvertible API subclass, it Is found that all subclasses have this method, so we use the idea to add an extension method named Is to String because TryPa The rse method is not implemented by the interface definition. While almost all the implementation of IConvertible interfaces in the C # system class library has the TryParse method. We define that the type T inherits the IConvertible; but it is also possible that the type T does not have the TryParse method or the method signature is not a string, in this case, we will throw a custom exception TryParseException. Note that when using reflection to find the TryParse method, the parameter type of the specified method is {typeof (string), typeof (T )} the query result is NULL. Because the second parameter declaration of TryParse is out, {typeof (string), typeof (T) should be used ). makeByRefType ()} you can find the copy code for this method. /// <summary> // abstract description of TypeValidate /// </summary> public static class TypeValidate {// <summary> // /Type T must have /// (1) Construction Method without parameters // (2) method signature to bool TryParse (string, T) // </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "value"> </param>/ // <returns> </returns> [SecuritySafeCritical] public static bool Is <T> (this string value) where T: IConvertible {var type = typeof (T); Type [] types = {typeof (string), type. makeByRefType ()}; var Method = type. getMethod ("TryParse", types); if (Meth Od = null) {throw new TryParseException ();} try {T Convertible = Activator. createInstance <T> (); return (bool) Method. invoke (Convertible, new object [] {value, Convertible}) ;}catch {throw new TryParseException ();}}} /// <summary> // The type does not have a method TryParse, no parameter constructor, or no method signature bool TryParse (String, T) // </summary> public class TryParseException: Exception {} copy the Code. This help class is complete. Let's try it! Test code copy code Response. write (string. format ("12131312 Is int = {0} <br/>", "12131312 ". is <int> (); Response. write (string. format ("121313123131231 Is int = {0} <br/>", "121313123131231 ". is <int> (); Response. write (string. format ("1231231231313123 Is int = {0} <br/>", "1231231231313123 ". is <int> (); Response. write (string. format ("adadasda Is int = {0} <br/>", "adadadasda ". is <int> (); Response. write (string. format ("true Is bool = {0} <br/>", "true ". is <bool> (); Response. write (string. format ("1 Is bool = {0} <br/>", "1 ". is <bool> (); Response. write (string. format ("06:06:06 Is Date = {0} <br/>", "06:06:06 ". is <DateTime> ())); copy the code result 12131312 Is int = True121313123131231 Is int = false12312312313123 Is int = Falseadadasda Is int = Falsetrue Is bool = True1 Is bool = False2013/4/5 06:06:06 Is Date = True

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.