扩展方法未编译(没有类型“字符串”定义)
我正在尝试使用下面的代码将字节转换为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于配额是一个字符串,所以必须先将其解析为数字:
由于数字太大,无法容纳 32 位整数,因此必须使用 Decimal:
也可以使用 Int64,但是然后该方法会截断结果,返回 3 GB,而不是 3.9 GB。
As the quota is a string, you have to parse it into a number first:
As the number is too large to fit in a 32 bit integer, you have to use a Decimal:
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.
这是因为
Value
是一个字符串,而扩展方法是为Int32
声明的。在调用扩展方法之前,您需要将Value
转换为Int32
。示例:
这也意味着应该为
Int64
声明扩展方法:This is because
Value
is a string, while the extension methods are declared forInt32
. You will need to convert theValue
to anInt32
prior to invoking the extension method.Example:
This also means that the extension methods should be declared for
Int64
:如果不知道事件参数中的内容,错误就相当简单。
字符串类型的 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()
orint.TryParse()
then you should have more luck.不过有几个错误.. 你应该除以 1024 ... 并将值转换为 Int .. 见下文。
希望这有帮助
A couple of errors though .. u should divide by 1024 instead ... and convert value to Int .. see below.
Hope this helps