加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#Strange Index Out Of Bounds异常

发布时间:2020-12-16 09:56:14 所属栏目:百科 来源:网络整理
导读:调试我正在为MS Outlook 2010开发的插件时,我遇到一个奇怪的问题,就是Index Out of Bounds异常.我有一个类进行消息处理,在该类的构造函数中,我传递了一个MailItem.然后,我打算运行MailItem的“收件人”列表,并查找在“收件人”,“抄送”和“密件抄送”字段中
调试我正在为MS Outlook 2010开发的插件时,我遇到一个奇怪的问题,就是Index Out of Bounds异常.我有一个类进行消息处理,在该类的构造函数中,我传递了一个MailItem.然后,我打算运行MailItem的“收件人”列表,并查找在“收件人”,“抄送”和“密件抄送”字段中注册的所有收件人.为此,我有以下代码:

public MessageProcessor(Outlook.MailItem theMail)
{
  _activeMailItem = theMail;
  _activeMailDetails.Sender = theMail.SenderEmailAddress;
  if (_activeMailItem.Recipients.Count > 0)
  {
    List<string> recipients = new List<string>();
    List<string> cc = new List<string>();
    List<string> bcc = new List<string>();
    for (int i = 0; i < _activeMailItem.Recipients.Count; i++)
    {
      switch (_activeMailItem.Recipients[i].Type)    <----- HERE
      {
        case (int)Outlook.OlMailRecipientType.olTo:
           recipients.Add(_activeMailItem.Recipients[i].Address);
           break;
        case (int)Outlook.OlMailRecipientType.olCC:
           cc.Add(_activeMailItem.Recipients[i].Address);
           break;
        case (int)Outlook.OlMailRecipientType.olBCC:
           bcc.Add(_activeMailItem.Recipients[i].Address);
           break;
      }
    }
  }
}

但是,我在标有“HERE”的点上得到了例外.当我调试并查看Recipients.Count属性的值时,它显示1.但是,当“i”索引为0(应该是有效索引 – 并且在这种情况下,唯一有效的索引)时会出现问题).当我尝试查看_activeMailItem.Recipients集合时,我看到Count为1;然而,当我试图在结构中进一步向下追踪时,我看到一些红色十字架,我无法检查下面的值.

有谁知道什么可能是错的?

提前致谢.

解决方法

Outlook中的所有集合(包括收件人)都是基于1而不是0:

for (int i = 1; i <= _activeMailItem.Recipients.Count; i++)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读