Some bits of code:.
C# version of IsNumeric( ) :public static bool IsNumeric(object value){
try {
int i = Convert.ToInt32(value.ToString());
return true;
}
catch (FormatException) {
return false;
}
}[via The Wagner Blog]一些位的代码: 。 IsNumeric()的C#版本: 公共静态布尔IsNumeric(对象值){ 尝试{ int i = Convert.ToInt32(value.ToString()); 返回true; } catch(FormatException){ 返回false; } } [通过Wagner博客]
Actually, this doesn't really do what IsNumeric does, as IsNumeric should also return true for floating point numbers. This is really more of an "IsInt".实际上,这实际上并没有实现IsNumeric的功能,因为IsNumeric对于浮点数也应返回true。 这实际上更像是一个“ IsInt”。Also, why write your own? You can just reference Microsoft.VisualBasic.dll, then do the following:另外,为什么要自己写? 您可以只引用Microsoft.VisualBasic.dll,然后执行以下操作:如果(Microsoft.VisualBasic.Information.IsNumeric(“ 5”)) { //做某事 }The VB one actually performs better, as it doesn't throw an exception for every failed conversion. [ Sean 'Early' Campbell & Scott 'Adopter' Swigart's Radio Weblog ]VB实际上表现更好,因为它不会为每次失败的转换引发异常。 [ Sean'Early'Campbell和Scott'Adopter'Swigart的广播博客 ]What's REALLY interesting is what's going on in the Source Code (disassembled) for the VisualBasic.Information.IsNumeric (as I am trepidacious to include this dependancy (dunno why) in my code).
真正有趣的是VisualBasic.Information.IsNumeric的源代码(反汇编)中发生的事情(因为我在代码中包含了这种依赖性(不知道为什么)),这真是令人惊讶。
Looks like it's pretty much the checking the Type of the object via iConvertable or the alternate TryParse...:
看起来几乎是通过iConvertable或备用TryParse ...检查对象的类型:
public static bool Isumeric (object Expression)
{
bool f;
ufloat64 a;
long l;公共静态布尔等值(对象表达式) { 布尔f; ufloat64 a; 长
IConvertible iConvertible = null;
if ( ((Expression is IConvertible)))
{
iConvertible = (IConvertible) Expression;
}iConvertible iConvertible = null; 如果(((Expression is IConvertible))) { iConvertible =(IConvertible)表达式; }
if (iConvertible == null)
{
if ( ((Expression is char[])))
{
Expression = new String ((char[]) Expression);
goto IL_002d; 'hopefully inserted by optimizer
}
return 0;
}
IL_002d:TypeCode typeCode = iConvertible.GetTypeCode ();if ((typeCode == 18) || (typeCode == 4))
{
string str = iConvertible.ToString (null);
try
{
if ( (StringType.IsHexOrOctValue (str, l)))
{
f = true;
return f;
}
}
catch (Exception )
{
f = false;
return f;
};return DoubleType.TryParse (str, a);}return Utils.IsNumericTypeCode (typeCode);}如果(iConvertible == null) { 如果(((表达式为char []))) { 表达式=新字符串((char [])表达式); 转到IL_002d; 希望由优化程序插入} 返回0; } IL_002d: TypeCode typeCode = iConvertible.GetTypeCode(); if(((typeCode == 18)||(typeCode == 4)) { 字符串str = iConvertible.ToString(null); 尝试{ 如果((StringType.IsHexOrOctValue(str,l))) { f =真; 返回f; } } 抓(Exception) { f =假; 返回f; }; 返回DoubleType.TryParse(str,a); } 返回Utils.IsNumericTypeCode(typeCode); }
internal static bool IsNumericType (Type typ)
{
bool f;
TypeCode typeCode;
if ( (typ.IsArray))
{
return 0;
}
switch (Type.GetTypeCode (typ))
{
case 3:
case 6:
case 7:
case 9:
case 11:
case 13:
case 14:
case 15:
return 1;
};
return 0;
}内部静态布尔IsNumericType(类型Typ) { 布尔f; TypeCode typeCode; 如果((typ.IsArray)) { 返回0; } 开关(Type.GetTypeCode(typ)) { 情况3: 情况6: 情况7: 案例9: 情况11: 案例13: 情况14: 案例15: 返回1; }; 返回0; }
翻译自: https://www.hanselman.com/blog/exploring-isnumeric-for-c