【问题标题】:Check if object can be cast to a certain value type?检查对象是否可以转换为某个值类型?
【发布时间】:2014-01-18 21:03:52
【问题描述】:

我有一个带有解析某个 JSON 的结果的哈希表:decodedJsondecodedJson["key"] 可以是 int、double、float、decimal 或字符串。如果它是一个数字,我需要将它转换为十进制(我打算用(decimal)decodedJson["key"] 来做),如果不是,则处理一个错误。

确定这一点的最有效方法是什么?

【问题讨论】:

    标签: c#


    【解决方案1】:
    if (decodedJson["key"] is decimal)
    {
    //do your action
    }
    

    【讨论】:

    • 是的,我们的想法很相似 :)
    【解决方案2】:

    由于它可以是任何数字类型,您可能需要:

    var i = decodedJson["key"];
    bool isNumeric = i is byte || i is sbyte || i is short || i is ushort || 
                     i is int || i is uint || i is long || i is ulong || 
                     i is float || i is double || i is decimal;
    
    if (isNumeric)
        Convert.ToDecimal(i);
    else
        //handle
    

    如果您想将其转换为不是实际底层类型的类型,简单的强制转换将不起作用Convert 类具有所需的综合测试。


    如果你愿意,也可以让它一直通用:

    public static T To<T>(this object source) where T : IConvertible
    {
        return (T)Convert.ChangeType(source, typeof(T));
    }
    
    public static bool IsNumeric(this Type t)
    {
        return t.In(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), 
                    typeof(int), typeof(uint), typeof(long), typeof(ulong), 
                    typeof(float), typeof(double), typeof(decimal));
    }
    
    public static bool In<T>(this T source, params T[] list)
    {
        return list.Contains(source);
    }
    

    或者找到更准确的IsNumerichere

    所以现在你打电话:

    var i = decodedJson["key"]; 
    if (i.GetType().IsNumeric())
        i.To<decimal>();
    else
        //handle
    

    【讨论】:

      【解决方案3】:

      如果对象是十进制的,你可以这样做

      if (decodedJson["key"] is decimal)
      {
         //Your code here
      }
      

      【讨论】:

        【解决方案4】:

        is 运算符可能是您的最佳选择

        http://msdn.microsoft.com/en-us/library/scekt9xw.aspx

            if(decodedJson["key"] is decimal){ 
        // do something 
        }
        

        【讨论】:

          【解决方案5】:

          【讨论】:

            【解决方案6】:

            根据问题,如果 decodedJson["key"] 可以是 int、float、double、decimal 或字符串中的任何内容。我们需要检查所有类型。

            if(decodedJson["key"] is int ||  decodedJson["key"] is float || decodedJson["key"] is double || decodedJson["key"] is decimal)
            {
                 decimal convereted_value = (decimal) decodedJson["key"];
                 // Rest of your code here 
            }
            else
            {
                 // Error handling code here.
            }
            

            【讨论】:

            • 考虑到如果类型是int,这又是有风险的,你不能转换为decimal 类型。 Convert class 是最好的选择。
            • 可能是 Convert 类是最好的选择,但你可以将 int 转换为十进制,看看这个小提琴dotnetfiddle.net/khCkUt
            • Niral,当然这是可能的。但是当值类型被装箱时,你不能转换它。例如,尝试:object i = 0; decimal d = (decimal)i;
            猜你喜欢
            • 1970-01-01
            • 2011-06-07
            • 1970-01-01
            • 2012-03-22
            • 2013-05-29
            • 2011-09-28
            • 1970-01-01
            • 1970-01-01
            • 2011-01-08
            相关资源
            最近更新 更多