您能否帮助我,并告诉我如何在C#中修复错误消息“索引超出数组范围的内容”?我将在下面发布代码。错误消息是:System.IndexOutOfRangeException:索引在第504和505行中的数组范围之外,我将此行标记为// THIS LINE
public void HandleConnectionData(ref byte[] data)
{
if (data[0] == 64 || data[0] == 33)
{
int pos = 0;
while (pos < data.Length)
{
try
{
if (data[pos] == 64)
{
int MessageLength = Base64Encoding.DecodeInt32(new byte[] { data[pos++], data[pos++], data[pos++] }); // THIS LINE
uint MessageId = Base64Encoding.DecodeUInt32(new byte[] { data[pos++], data[pos++] }); // THIS LINE
if ((MessageLength - 2) > (data.Length - pos))
{
return;
}
byte[] Content;
try
{
Content = new byte[MessageLength - 2];
for (int i = 0; i < Content.Length; i++)
{
Content[i] = data[pos++];
}
}
catch (Exception e)
{
Logging.LogException("HandleConnectionData #1 Error: " + e.ToString());
return;
}
ClientMessage Message = new ClientMessage(MessageId, Content);
if (this.MessageHandler == null)
{
this.InitHandler();
}
if (Message != null)
{
try
{
Logging.LogPackets2("[" + this.UInt32_0 + "] --> [" + Message.Id + "] " + Message.Header + Message.GetBody());
if (int.Parse(Phoenix.GetConfig().data["debug"]) == 1)
{
Logging.WriteLine("[" + this.UInt32_0 + "] --> [" + Message.Id + "] " + Message.Header + Message.GetBody());
}
}
catch { }
Interface @interface;
if (Phoenix.smethod_10().Handle(Message.Id, out @interface))
{
@interface.Handle(this, Message);
}
}
}
else if (data[pos] == 33)
{
++pos;
int MessageLength = Base64Encoding.DecodeInt32(new byte[] { data[pos++], data[pos++], data[pos++] });
if (MessageLength > (data.Length - pos))
{
return;
}
byte[] Content;
try
{
Content = new byte[MessageLength];
for (int i = 0; i < Content.Length; i++)
{
Content[i] = data[pos++];
}
}
catch (Exception e)
{
Logging.LogException("HandleConnectionData #2 Error: " + e.ToString());
return;
}
try
{
CustomEventHandler.HandleEvent(this, System.Text.Encoding.UTF8.GetString(Content));
}
catch (Exception e)
{
Logging.LogException("HandleConnectionData #3 Error: " + e.ToString());
}
}
}
catch (Exception e)
{
Logging.LogException("Error #1: " + e.ToString());
this.Disconnect();
}
}
}
else if (data[0] == '<')
{
this.Connection.SendData(CrossdomainPolicy.GetXmlPolicy());
this.Connection.Dispose();
}
}