扩展方法不编译(没有类型'string'的定义)

时间:2011-01-11 09:25:42

标签: c# linq itemssource

我正在尝试使用下面的代码将字节转换为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'的定义,并且没有扩展方法'BytesToGigabytes'接受类型'string'的第一个参数可以找到(你是否缺少using指令或汇编引用?)”< / p>

4 个答案:

答案 0 :(得分: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。

答案 1 :(得分:2)

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

示例:

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) 

答案 2 :(得分:1)

在不了解事件参数中的最新情况时,错误相当简单。

对于一种字符串,没有BytesToGigabytes的扩展名。

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

答案 3 :(得分: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(),                                   };
    }

希望这有帮助

相关问题