是否有尝试Convert.ToInt32 ...避免异常 [英] Is there a try Convert.ToInt32... avoiding exceptions

查看:379
本文介绍了是否有尝试Convert.ToInt32 ...避免异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否存在将对象转换为 int 的安全方法,避免出现异常。



我正在寻找类似 public static bool TryToInt32(object value,out int result);


$ b $的东西b

我知道我可以做这样的事情:

  public static bool TryToInt32(对象值,出int结果)
{
try
{
结果= Convert.ToInt32(值);
返回true;
}
catch
{
结果= 0;
返回false;
}
}

但我宁愿避免出现异常,因为它们



我认为这比较优雅,但仍然便宜:

 公共静态布尔TryToInt32(对象值,输出为int结果)
{
if(value == null)
{
result = 0;
返回false;
}

返回int.TryParse(value.ToString(),超出结果);
}

有人有更好的主意吗?



更新:



这听起来有点像割头发,但是将对象转换为字符串会强制实现者创建清晰的 ToString ()函数。例如:

  public class百分比
{
public int Value {get;组; }

公共替代字符串ToString()
{
返回string.Format( {0}%,Value);
}
}

百分比p =新的Percentage();
p.Value = 50;

int v;
if(int.TryParse(p.ToString(),out v))
{

}

这是错误的,我可以在这里做两件事,或者像这样实现 IConvertable

 公共静态布尔值ToInt32(对象值,输出结果为int)
{
if(value == null)
{
结果= 0;
返回false;
}

if(值是IConvertible)
{
结果=((IConvertible)value).ToInt32(Thread.CurrentThread.CurrentCulture);
返回true;
}

返回int.TryParse(value.ToString(),超出结果);
}

但是 ToInt32 IConvertible 的方法无法取消。因此,如果无法转换该值,则无法避免异常。



或者两个:有没有一种方法可以检查对象是否包含隐式运算符? p>

这非常糟糕:

  if(value.GetType()。 GetMethods()。FirstOrDefault(方法=>方法。名称== op_Implicit&&方法.ReturnType == typeof(int))!= null)
{
结果=(int)值;
返回true;
}


解决方案

  int变量= 0; 
int.TryParse(stringValue,out变量);

如果无法解析,则变量为0。请参见 http://msdn.microsoft.com/en-us/library/f02979c7.aspx

I'd like to know if there is a "safe" way to convert an object to an int, avoiding exceptions.

I'm looking for something like public static bool TryToInt32(object value, out int result);

I know I could make something like this:

public static bool TryToInt32(object value, out int result)
{
    try
    {
        result = Convert.ToInt32(value);
        return true;
    }
    catch
    {
        result = 0;
        return false;
    }
}

But I'd rather avoid exceptions, because they are slowing down the process.

I think this is more elegant, but it's still "cheap":

public static bool TryToInt32(object value, out int result)
{
    if (value == null)
    {
        result = 0;
        return false;
    }

    return int.TryParse(value.ToString(), out result);
}

Does anyone have better ideas?

UPDATE:

This sounds a little like splitting hairs, but converting an object to string forces the implementer to create a clear ToString() function. For example:

public class Percentage
{
    public int Value { get; set; }

    public override string ToString()
    {
        return string.Format("{0}%", Value);
    }
}

Percentage p = new Percentage();
p.Value = 50;

int v;
if (int.TryParse(p.ToString(), out v))
{

}

This goes wrong, I can do two things here, or implement the IConvertable like this:

public static bool ToInt32(object value, out int result)
{
    if (value == null)
    {
        result = 0;
        return false;
    }

    if (value is IConvertible)
    {
        result = ((IConvertible)value).ToInt32(Thread.CurrentThread.CurrentCulture);
        return true;
    }

    return int.TryParse(value.ToString(), out result);
}

But the ToInt32 method of the IConvertible cannot be canceled. So if it's not possible to convert the value, an exception cannot be avoided.

Or two: Is there a way to check if the object contains a implicit operator?

This is very poor:

if (value.GetType().GetMethods().FirstOrDefault(method => method.Name == "op_Implicit" && method.ReturnType == typeof(int)) != null)
{
    result = (int)value;
    return true;
}

解决方案

int variable = 0;
int.TryParse(stringValue, out variable);

If it can't be parsed, the variable will be 0. See http://msdn.microsoft.com/en-us/library/f02979c7.aspx

这篇关于是否有尝试Convert.ToInt32 ...避免异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆