我需要从日历中获取所有AppointmentItems,但是在几个项目之后,我得到了一个异常,错误是打开的项目的最大数量是有限的。我不通过foreach循环来循环它们,所以引用应该没有问题。我立即将这些项目“转换”为我的项目类型,以便释放对象。在释放对象并尝试调用垃圾收集器之后,我还将对象的引用设置为null,但错误仍然发生。
Items items = application.Session.GetFolderFromID(MainCalendar).Items;
for(Int32 i = 1; i < items.Count; i++)
{
AppointmentItem appointmentItem = items[i] as AppointmentItem;
TempOutlookAppointment tempApp = new TempOutlookAppointment();
tempApp.FillFromAppointmentItem(appointmentItem);
appointmentItem.Close(OlInspectorClose.olDiscard);
Marshal.ReleaseComObject(appointmentItem);
appointmentItem = null;
GC.Collect();
// Adding tempApp to a List
}
// FillFromAppointmentItem
public void FillFromAppointmentItem(Microsoft.Office.Interop.Outlook.AppointmentItem appointmentItem)
{
AllDayEvent = appointmentItem.AllDayEvent;
Body = appointmentItem.Body;
End = appointmentItem.End;
Location = appointmentItem.Location;
OutlookLinkID = appointmentItem.UserProperties.Find("LINKID") != null
? Guid.Parse(appointmentItem.UserProperties.Find("LINKID").Value.ToString())
: Guid.Empty;
Start = appointmentItem.Start;
if (appointmentItem.IsRecurring)
{
Microsoft.Office.Interop.Outlook.RecurrencePattern pattern = appointmentItem.GetRecurrencePattern();
TerminSerie serie = new TerminSerie()
{
MaxOccurences = pattern.Occurrences,
PatternDayOfMonth = pattern.DayOfMonth,
PatternFrequency = pattern.RecurrenceType == Microsoft.Office.Interop.Outlook.OlRecurrenceType.olRecursDaily ? PatternFrequency.Daily : pattern.RecurrenceType == Microsoft.Office.Interop.Outlook.OlRecurrenceType.olRecursMonthly ? PatternFrequency.Monthly : pattern.RecurrenceType == Microsoft.Office.Interop.Outlook.OlRecurrenceType.olRecursWeekly ? PatternFrequency.Weekly : PatternFrequency.Yearly,
PatternInterval = pattern.RecurrenceType == Microsoft.Office.Interop.Outlook.OlRecurrenceType.olRecursMonthly ? pattern.Interval / 12 : pattern.Interval,
PatternMonthOfYear = pattern.MonthOfYear,
RangeEndDate = pattern.PatternEndDate,
RangeLimit = pattern.NoEndDate ? RangeLimit.NoLimit : pattern.Occurrences > 0 ? RangeLimit.ByOccurences : RangeLimit.ByDate,
RangeStartDate = pattern.PatternStartDate
};
Int32 i = pattern.DayOfWeekMask.GetHashCode();
List<System.DayOfWeek> days = new List<System.DayOfWeek>();
if (i >= 64)
{
days.Add(System.DayOfWeek.Saturday);
i -= 64;
}
if (i >= 32)
{
days.Add(System.DayOfWeek.Friday);
i -= 32;
}
if (i >= 16)
{
days.Add(System.DayOfWeek.Thursday);
i -= 16;
}
if (i >= 8)
{
days.Add(System.DayOfWeek.Wednesday);
i -= 8;
}
if (i >= 4)
{
days.Add(System.DayOfWeek.Tuesday);
i -= 4;
}
if (i >= 2)
{
days.Add(System.DayOfWeek.Monday);
i -= 2;
}
if (i == 1)
{
days.Add(System.DayOfWeek.Sunday);
}
serie.PatternDaysOfWeek = days;
}
Subject = appointmentItem.Subject;
}