English 中文(简体)
不汇编的推广方法(对类型扼杀没有定义)
原标题:Extension Method not compiling (No defintion for type string )

然而,我试图用以下代码将tes子转换成KB/MB/GB,但我似乎无法开始工作。 配额价值为600 000。

    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:

“说明中并未包含对Bytes ToGigabytes的定义,也没有发现任何通过ToGigabytes接受第一种类型扼杀论点的延伸方法(如果你没有指令或集会参考?)”

最佳回答

由于配额是扼杀性的,你必须首先将其分成若干部分:

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

由于这个数目太大,无法适应32个轨道分类,因此,你不得不使用“ Dec”:

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

也可以使用Int64,但这种方法将减少结果,例如将3 GB退回,而不是3.9 GB。

问题回答

这是因为<代码>Value是一种指示,而延伸方法则被宣布为<代码>Int<32/code>。 您在援引推广方法之前需要将<代码>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) 

如果不了解你的情况,错误就相当直截了当。

不允许将拜特人ToGigabytes延伸到某种扼杀。

So query. 部分(“报价”)正在恢复。 如果是(int.Parse(>)或int.TryParse(),那么你就应当有更多的uck。

但有两点错误,即“u”应分出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(),                                   };
    }

希望这一帮助





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签