2

我正在尝试使用下面的代码将字节转换为 KB/MB/GB,但是,我似乎无法让它工作。配额的值为 60000000000。

    public static double BytesToKilobytes(this Int32 bytes)
    {
        return bytes / 1000d;
    }

    public static double BytesToMegabytes(this Int32 bytes)
    {
        return bytes / 1000d / 1000d;
    }

    public static double BytesToGigabytes(this Int32 bytes)
    {
        return bytes / 1000d / 1000d / 1000d;
    }

    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
            return;

        XDocument xDocument = XDocument.Parse(e.Result);

        listBox1.ItemsSource = from query in xDocument.Descendants("service")
                               select new Service
                               {
                                   type = query.Attribute("type").Value,
                                   id = query.Element("id").Value,
                                   plan = query.Element("username").Value,
                                   quota = query.Element("quota").Value.BytesToGigabytes,                                   };
    }

上面代码产生的错误是:

“'string' 不包含'BytesToGigabytes' 的定义,并且找不到接受'string' 类型的第一个参数的扩展方法'BytesToGigabytes'(您是否缺少 using 指令或程序集引用?)”

4

4 回答 4

5

由于配额是一个字符串,您必须先将其解析为一个数字:

quota = Decimal.Parse(query.Element("quota").Value).BytesToGigabytes()

由于数字太大而无法放入 32 位整数,因此您必须使用小数:

public static Decimal BytesToGigabytes(this Decimal bytes) {
  return bytes / 1000m / 1000m / 1000m;
}

也可以使用 Int64,但该方法会截断结果,例如返回 3 GB 而不是 3.9 GB。

于 2011-01-11T09:32:02.210 回答
2

这是因为Value是一个字符串,而扩展方法是为Int32. 在调用扩展方法之前,您需要将 转换为ValueInt32

例子:

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    Func<string, Int64> convertToInt64 = s =>
    {
       Int64 result;
            // replace 0 with whatever default you want
       return Int64.TryParse(s, out result) ? result : 0;
    };
    if (e.Error != null) return; 
    XDocument xDocument = XDocument.Parse(e.Result); 
    listBox1.ItemsSource = from query in xDocument.Descendants("service") 
                           select new Service 
                           { 
                               type = query.Attribute("type").Value, 
                               id = query.Element("id").Value, 
                               plan = query.Element("username").Value, 
                               quota = convertToInt64(query.Element("quota").Value)
                                           .BytesToKilobytes()
                           };
}

这也意味着应该声明扩展方法Int64

public static double BytesToKilobytes(this Int64 bytes) 
于 2011-01-11T09:29:09.270 回答
1

在不知道您的事件参数的情况下,该错误相当简单。

对于字符串类型,BytesToGigabytes 没有扩展名。

所以 query.Element("quota") 返回一个字符串。如果你解析它(int.Parse()或者int.TryParse()你应该有更多的运气。

于 2011-01-11T09:29:34.613 回答
0

虽然有几个错误.. 你应该除以 1024 ...并将值转换为 Int .. 见下文。

 public static double BytesToKilobytes(this Int32 bytes)
    {
        return bytes / 1024d;
    }

    public static double BytesToMegabytes(this Int32 bytes)
    {
        return bytes / 1024d / 1024d;
    }

    public static double BytesToGigabytes(this Int32 bytes)
    {
        return bytes / 1024d / 1024d / 1024d;
    }

    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
            return;

        XDocument xDocument = XDocument.Parse(e.Result);

        listBox1.ItemsSource = from query in xDocument.Descendants("service")
                               select new Service
                               {
                                   type = query.Attribute("type").Value,
                                   id = query.Element("id").Value,
                                   plan = query.Element("username").Value,
                                   quota = Convert.ToInt32(query.Element("quota").Value).BytesToGigabytes(),                                   };
    }

希望这可以帮助

于 2011-01-11T09:30:24.750 回答