【问题标题】:C#.NET IntPtr Invalid TypeC#.NET IntPtr 无效类型
【发布时间】:2013-08-14 05:57:46
【问题描述】:
private string getLngLat(string strLAC, string strCID)
    {
        string str;
        try
        {
            HttpWebRequest length = (HttpWebRequest)WebRequest.Create(new Uri("http://www.google.com/glm/mmap"));
            length.Method = "POST";
            int num = Convert.ToInt32(strLAC);
            byte[] numArray = AjaxFrm.PostData(num, Convert.ToInt32(strCID));
            length.ContentLength = (long)((int)numArray.Length);
            length.ContentType = "application/binary";
            length.GetRequestStream().Write(numArray, 0, (int)numArray.Length);
            HttpWebResponse response = (HttpWebResponse)length.GetResponse();
            byte[] numArray1 = new byte[checked((IntPtr)response.ContentLength)]; // ERROR AT HERE
            response.GetResponseStream().Read(numArray1, 0, (int)numArray1.Length);
            if (response.StatusCode != HttpStatusCode.OK)
            {
                str = string.Format("Error {0}", response.StatusCode);
            }
            else
            {
                byte num1 = numArray1[0];
                byte num2 = numArray1[1];
                byte num3 = numArray1[2];
                if ((numArray1[3] << 24 | numArray1[4] << 16 | numArray1[5] << 8 | numArray1[6]) != 0)
                {
                    str = "";
                }
                else
                {
                    double num4 = (double)(numArray1[7] << 24 | numArray1[8] << 16 | numArray1[9] << 8 | numArray1[10]) / 1000000;
                    double num5 = (double)(numArray1[11] << 24 | numArray1[12] << 16 | numArray1[13] << 8 | numArray1[14]) / 1000000;
                    str = string.Format("{0}&{1}", num5, num4);
                }
            }
        }
        catch (Exception exception)
        {
            str = string.Format("Error {0}", exception.Message);
        }
        return str;
    }

byte[] numArray1 = new byte[checked((IntPtr)response.ContentLength)];

在这里我得到了错误

Error 3 Cannot implicitly convert type 'System.IntPtr' to 'int'. An explicit conversion exists (are you missing a cast?)

怎么会出现这个错误?如何解决?

【问题讨论】:

  • 你确定你的意思不是long?顺便说一句,这让我的眼睛流血了。
  • 你为什么要把ContentLength 转换成IntPtr
  • 你应该发布checked()函数的代码,也许有问题
  • @WiiMaxx: checked 不是函数 - 它是 C# 中使用检查算术和转换的关键字。

标签: c# intptr


【解决方案1】:

直接指定数组长度即可,无需先尝试转换为IntPtr

byte[] numArray1 = new byte[response.ContentLength];

IntPtr 是一种仅用于保存指针值的类型,因此不适用整数类型的通常规则。指针值不是合理的数组长度,因此语言设计者不允许使用IntPtr 指定数组长度。

【讨论】:

  • 你错过了checked
  • @ChinYe checked 没有在此处添加任何内容,但如果您不相信,请编辑您的问题以阐明您认为需要它的原因,我将编辑我的答案以解决这一点。
【解决方案2】:

如果您的目标是转换为 int,您只需这样做:

byte[] numArray1 = new byte[checked((int)response.ContentLength)];

这将避免您尝试为 numArray1 分配超过 2GB 的空间。不过,您不需要从 编译器 的角度执行此操作 - 您可以使用 long 的长度值来分配数组。只是如果大小足够大,它会在执行时倒下。 (在 .NET 4.5 中,您可以使用应用程序配置设置来允许总大小超过 2GB 的数组,但我不确定这是否允许 elements 的数量超过 @ 987654325@.)

所以实际上,最好将检查留给 CLR:

byte[] numArray1 = new byte[response.ContentLength];

... 或者如果您确实想要施加一些最大尺寸,请明确执行此操作。 (我也建议最大小于 2GB...)

您还应该考虑ContentLength 为-1 的可能性,表明客户端没有在响应标头中指定它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多