C#.NET IntPtr无效类型 私有字符串getLngLat(字符串strLAC,字符串strCID) { 字符串str; 尝试 { HttpWebRequest长度=(HttpWebRequest)WebRequest.Create(新Uri(“http://www.google.com/glm/mmap")); length.Method=“POST”; int num=Convert.ToInt32(strLAC); 字节[]numArray=AjaxFrm.PostData(num,Convert.ToInt32(strCID)); length.ContentLength=(long)((int)numArray.length); length.ContentType=“应用程序/二进制”; length.GetRequestStream().Write(numArray,0,(int)numArray.length); HttpWebResponse=(HttpWebResponse)length.GetResponse(); byte[]numaray1=新字节[已检查((IntPtr)response.ContentLength)];//此处有错误 Read(numArray1,0,(int)numArray1.Length); if(response.StatusCode!=HttpStatusCode.OK) { str=string.Format(“错误{0}”,response.StatusCode); } 其他的 { 字节num1=numArray1[0]; 字节num2=numArray1[1]; 字节num3=numArray1[2]; 如果((numaray1[3]
只需直接指定数组长度,则无需首先尝试转换为C#.NET IntPtr无效类型 私有字符串getLngLat(字符串strLAC,字符串strCID) { 字符串str; 尝试 { HttpWebRequest长度=(HttpWebRequest)WebRequest.Create(新Uri(“http://www.google.com/glm/mmap")); length.Method=“POST”; int num=Convert.ToInt32(strLAC); 字节[]numArray=AjaxFrm.PostData(num,Convert.ToInt32(strCID)); length.ContentLength=(long)((int)numArray.length); length.ContentType=“应用程序/二进制”; length.GetRequestStream().Write(numArray,0,(int)numArray.length); HttpWebResponse=(HttpWebResponse)length.GetResponse(); byte[]numaray1=新字节[已检查((IntPtr)response.ContentLength)];//此处有错误 Read(numArray1,0,(int)numArray1.Length); if(response.StatusCode!=HttpStatusCode.OK) { str=string.Format(“错误{0}”,response.StatusCode); } 其他的 { 字节num1=numArray1[0]; 字节num2=numArray1[1]; 字节num3=numArray1[2]; 如果((numaray1[3],c#,intptr,C#,Intptr,只需直接指定数组长度,则无需首先尝试转换为IntPtr: private string getLngLat(string strLAC, string strCID) { string str; try { HttpWebRequest length = (HttpWebRequest)WebRequest.Create(new Uri("http://www.google.com/glm/mmap"));
IntPtr
:
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;
}
IntPtr
是一种仅用于保存指针值的类型,因此积分类型的常规规则不适用。指针值不是合理的数组长度,因此语言设计者不允许使用IntPtr
指定数组长度。如果您的目标是转换为int
,则只需在代替:
byte[] numArray1 = new byte[response.ContentLength];
这将避免您尝试为numArray1
分配超过2GB的内存。不过,您不需要从编译器的角度执行此操作-您可以使用long
长度值分配数组。只是如果大小足够大,它将在执行时失效。(在.NET 4.5中,您可以使用应用程序配置设置来允许总大小超过2GB的阵列,但我不确定这是否允许元素数量超过int.MaxValue
)
因此,实际上,您最好将检查留给CLR:
byte[] numArray1 = new byte[checked((int)response.ContentLength)];
…或者,如果您确实想施加一些最大大小,请明确地这样做。(我建议最大大小也应小于2GB…)
你也应该考虑代码< > Cordent Stime//Cube为-1的可能性,表示客户端没有在响应头中指定它。
?你确定你不是指<代码>长< /代码>吗?这使我的眼睛流血BTW。为什么你一开始就把代码> Client Stime/Cuth>转换成<代码> ItpTrt/COD>?你应该发布你的代码FO。r已选中()
功能可能有problem@WiiMaxx:checked
不是一个函数-它是C#中的一个关键字,用于使用checked算术和转换。你错过了checked
?@ChinYechecked
没有在这里添加任何内容,但如果你不这么认为,请编辑你的问题,澄清你为什么认为你需要它我将编辑我的答案以解决这一点。
byte[] numArray1 = new byte[response.ContentLength];