扩展方法未编译(没有类型“字符串”定义)

发布于 2024-10-11 06:10:24 字数 1276 浏览 3 评论 0原文

我正在尝试使用下面的代码将字节转换为 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 指令或程序集引用吗?)”

I'm trying to convert bytes into KB/MB/GB using the code below, however, I can't seem to get it working. The value of quota, is 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,                                   };
    }

The error the above code produces is:

"'string' does not contain a definition for 'BytesToGigabytes' and no extension method 'BytesToGigabytes' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荆棘i 2024-10-18 06:10:24

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

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

由于数字太大,无法容纳 32 位整数,因此必须使用 Decimal:

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

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

As the quota is a string, you have to parse it into a number first:

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

As the number is too large to fit in a 32 bit integer, you have to use a Decimal:

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

It would also be possible to use an Int64, but then the method would truncate the result, returning for example 3 GB instead of 3.9 GB.

剩一世无双 2024-10-18 06:10:24

这是因为 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) 

This is because Value is a string, while the extension methods are declared for Int32. You will need to convert the Value to an Int32 prior to invoking the extension method.

Example:

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()
                           };
}

This also means that the extension methods should be declared for Int64:

public static double BytesToKilobytes(this Int64 bytes) 
小傻瓜 2024-10-18 06:10:24

如果不知道事件参数中的内容,错误就相当简单。

字符串类型的 BytesToGigabytes 没有扩展名。

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

Without knowing whats in your event args, the error is fairly straightforward.

There is no extension for a BytesToGigabytes for a type of string.

So query.Element("quota") is returning a string. If you parse it (int.Parse() or int.TryParse() then you should have more luck.

撑一把青伞 2024-10-18 06:10:24

不过有几个错误.. 你应该除以 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(),                                   };
    }

希望这有帮助

A couple of errors though .. u should divide by 1024 instead ... and convert value to Int .. see below.

 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(),                                   };
    }

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文