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 ビット整数に収まらないため、Decimal を使用する必要があります。

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

Int64 を使用することもできますが、メソッドは結果を切り捨て、たとえば 3.9 GB ではなく 3 GB を返します。

于 2011-01-11T09:32:02.210 に答える
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) 
于 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 に答える