In your work, you often encounter validating that the value in string is of type int or bool or date
The general practice is to use the method type. TryParse (string, type) for verification.
string strvalue = "123";
int intvalue;
BOOL Isint = Int. TryParse (strvalue, out intvalue);
However, you need to create a variable of that type using this method. I work in the Web program often a page to verify that the value type from the foreground is 10, which is a bit of a hassle. Because we just have to decide if the value of string is the right type.
I found this tryparse method basic BOOL int date These types all exist but look for them without this method in their interface. And then I went through the IConvertible interface subclass found that all subclasses have this method
So I used the idea to add a string with an extension method named is
Because the TryParse method does not implement an interface definition. But the C # System class Library implements the IConvertible interface almost all has the TryParse method we define the type T to inherit iconvertible;
But it is also possible that type T has no TryParse method or method signature not string,out T
In this case we throw a custom exception tryparseexception
Also note that the parameter type of the specified method when using reflection to find the TryParse method is {typeof (String), typeof (T)} query result is NULL because the second parameter declaration of TryParse is out, to use the {typeof (string ), typeof (T). Makebyreftype ()} to find this method
<summary>///typevalidate Summary description///</summary> public static class Typevalidate {///<summary>
; Type T must have///(1) Parameterless construction method///(2) method signed as 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 ty
PE = typeof (T); Type[] types = {typeof (String), type.
Makebyreftype ()}; var method = type.
GetMethod ("TryParse", types);
if (method = = null) {throw new tryparseexception ();
try {T convertible = activator.createinstance<t> ();
return (BOOL) Method.invoke (convertible, new object[] {value, convertible});
Catch {throw new tryparseexception (); }}}///<summary>///type has no method TryParse or no parameterless construction method or no method signature bool TryParse (string,t)///</summary> public class TryParse Exception:exception {}
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/
This help class even if it is finished let's try to effect it!
Test 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/>", "ADADASDA".) 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 ("2013/4/5 06:06:06 is Date ={0}<br/>", "2013/4/5 06:06:06". Is<datetime> ()));
Results
12131312 is int =true
121313123131231 is int =false
1231231231313123 is int =false
ADADASDA is int =false
True is bool =true
1 is bool =false
2013/4/5 06:06:06 is Date =true