/// <summary> /// Returns the number of elements in a sequence /// </summary> /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam> /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param> /// <param name="predicate">a function to test each element for a condition</param> /// <param name="append">append items in sequence to parent instance in com proxy management</param> /// <returns>the number of elements in the input sequence.</returns> /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception> /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception> public static int Count <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, bool> predicate, bool append = true) { if (source == null) { throw new ArgumentNullException("source"); } int result = 0; ICOMObject enumerator = null; try { enumerator = source.GetComObjectEnumerator(null); foreach (TSource current in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator)) { if (predicate(current)) { result++; } TryDispose(current); } if (null != enumerator && enumerator != source) { enumerator.Dispose(); } } catch { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } throw; } return(result); }
/// <summary> /// Returns the first element of a sequence /// </summary> /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam> /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param> /// <param name="append">append items in sequence to parent instance in com proxy management</param> /// <returns>the first element in the specified sequence</returns> /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception> /// <exception cref="InvalidOperationException">sequence is empty</exception> /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception> public static TSource First <TSource>(this IEnumerableProvider <TSource> source, bool append = true) { if (source == null) { throw new ArgumentNullException("source"); } ICOMObject enumerator = null; try { enumerator = source.GetComObjectEnumerator(null); foreach (TSource item in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator)) { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } return(item); } throw new InvalidOperationException("Sequence is empty."); } catch { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } throw; } }
/// <summary> /// Filters a sequence of values based on a predicate /// </summary> /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam> /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param> /// <param name="predicate">a function to test each element for a condition</param> /// <param name="append">append items in sequence to parent instance in com proxy management</param> /// <returns>true if the source sequence contains an element that has the specified value; otherwise, false</returns> /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception> /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception> public static IEnumerable <TSource> Where <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, int, bool> predicate, bool append = true) { if (null == source) { throw new ArgumentNullException("source"); } if (null == predicate) { throw new ArgumentNullException("predicate"); } ICOMObject enumerator = null; List <TSource> list = new List <TSource>(); try { enumerator = source.GetComObjectEnumerator(null); int num = -1; foreach (TSource item in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator)) { int num2 = num; num = checked (num2 + 1); if (predicate(item, num)) { list.Add(item); } else { TryDispose(item); } } if (null != enumerator && enumerator != source) { enumerator.Dispose(); } return(list); } catch { for (int i = list.Count - 1; i >= 0; i++) { TryDispose(list[i]); } if (null != enumerator && enumerator != source) { enumerator.Dispose(); } throw; } }
/// <summary> /// Returns the last element of a sequence /// </summary> /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam> /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param> /// <param name="append">append items in sequence to parent instance in com proxy management</param> /// <param name="predicate">a function to test each element for a condition</param> /// <returns>the value at the last position in the source sequence</returns> /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception> /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception> public static TSource LastOrDefault <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, bool> predicate, bool append = true) { if (source == null) { throw new ArgumentNullException("source"); } ICOMObject enumerator = null; try { enumerator = source.GetComObjectEnumerator(null); IEnumerator enumeratorSource = source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator).GetEnumerator(); if (enumeratorSource.MoveNext()) { TSource lastCurrent = default(TSource); TSource current = default(TSource); do { lastCurrent = current; TSource item = (TSource)enumeratorSource.Current; if (predicate(item)) { current = item; } if (null != lastCurrent) { TryDispose(lastCurrent); } }while (enumeratorSource.MoveNext()); if (null != current) { return(current); } } if (null != enumerator && enumerator != source) { enumerator.Dispose(); } return(default(TSource)); } catch { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } throw; } }
// -- Best Practice sample extension to create extensions for IEnumerable<T> in NetOffice // // In order to prevent ambiguous conflicts // you need to target NetOffice.CollectionsGeneric.IEnumerableProvider<T> // All collections in NetOffice implement these interface public static IEnumerable <Excel.Range> AllCellsWithValues(this IEnumerableProvider <Excel.Range> source) { List <Excel.Range> result = new List <NetOffice.ExcelApi.Range>(); ICOMObject enumerator = source.GetComObjectEnumerator(null); try { foreach (Excel.Range item in source.FetchVariantComObjectEnumerator(source as ICOMObject, enumerator)) { if (item.Value != null) { result.Add(item); } else { item.Dispose(); } } return(result); } catch { throw; } finally { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } } }
/// <summary> /// Returns the first element of a sequence /// </summary> /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam> /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param> /// <param name="append">append items in sequence to parent instance in com proxy management</param> /// <param name="predicate">a function to test each element for a condition</param> /// <returns>the first element in the specified sequence</returns> /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception> /// <exception cref="InvalidOperationException">sequence is empty</exception> /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception> public static TSource First <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, bool> predicate, bool append = true) { if (source == null) { throw new ArgumentNullException("source"); } ICOMObject enumerator = null; try { IEnumerableProvider <TSource> sequence = source as IEnumerableProvider <TSource>; if (null == sequence) { throw new ArgumentException("Unable to cast IEnumerableProvider<TSource>"); } enumerator = sequence.GetComObjectEnumerator(null); foreach (TSource item in sequence.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator)) { if (predicate(item)) { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } return(item); } else { TryDispose(item); } } if (null != enumerator && enumerator != source) { enumerator.Dispose(); } throw new InvalidOperationException("No element satisfies the condition in predicate or the source sequence is empty."); } catch { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } throw; } }
private static void TryDispose(object item) { ICOMObject comObject = item as ICOMObject; if (null != comObject) { comObject.Dispose(); } }
/// <summary> /// Returns the first element of a sequence, or a default value if the sequence contains no elements /// </summary> /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam> /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param> /// <param name="predicate">a function to test each element for a condition</param> /// <param name="append">append items in sequence to parent instance in com proxy management</param> /// <returns>default(TSource) if <paramref name="source" /> is empty; otherwise, the first element in <paramref name="source" /></returns> /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception> /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception> public static TSource FirstOrDefault <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, bool> predicate, bool append = true) { if (source == null) { throw new ArgumentNullException("source"); } ICOMObject enumerator = null; try { enumerator = source.GetComObjectEnumerator(null); foreach (TSource item in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator)) { if (predicate(item)) { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } return(item); } else { TryDispose(item); } } if (null != enumerator && enumerator != source) { enumerator.Dispose(); } return(default(TSource)); } catch { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } throw; } }
public void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom) { try { if (null != _application) { _application.Dispose(); } } catch (Exception exception) { OfficeRegistry.LogErrorMessage(_hostApplicationName, _progId, "Error occured in OnDisconnection. ", exception); } }
/// <summary> /// Determines whether a sequence contains any elements /// </summary> /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam> /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param> /// <param name="append">append items in sequence to parent instance in com proxy management</param> /// <returns>true if the source sequence contains any elements; otherwise, false</returns> /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception> /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception> public static bool Any <TSource>(this IEnumerableProvider <TSource> source, bool append = true) { if (source == null) { throw new ArgumentNullException("source"); } ICOMObject enumerator = null; try { IEnumerableProvider <TSource> sequence = source as IEnumerableProvider <TSource>; if (null == sequence) { throw new ArgumentException("Unable to cast IEnumerableProvider<TSource>"); } enumerator = sequence.GetComObjectEnumerator(null); foreach (TSource current in sequence.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator)) { TryDispose(current); return(true); } if (null != enumerator && enumerator != source) { enumerator.Dispose(); } return(false); } catch { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } throw; } }
/// <summary> /// Determines whether a sequence contains a specified element by using the NetOffice Core equality comparer /// </summary> /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam> /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param> /// <param name="value">the value to locate in the sequence</param> /// <param name="append">append items in sequence to parent instance in com proxy management</param> /// <returns>true if the source sequence contains an element that has the specified value; otherwise, false</returns> /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception> /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception> public static bool Contains <TSource>(this IEnumerableProvider <TSource> source, TSource value, bool append = true) { if (source == null) { throw new ArgumentNullException("source"); } ICOMObject enumerator = null; try { enumerator = source.GetComObjectEnumerator(null); foreach (TSource current in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator)) { bool match = NetOffice.CoreServices.RemoteComparsion.EqualsOnServer(current, value); TryDispose(current); if (match) { return(true); } } if (null != enumerator && enumerator != source) { enumerator.Dispose(); } return(false); } catch { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } throw; } }
/// <summary> /// Determines whether a sequence contains any elements /// </summary> /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam> /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param> /// <param name="predicate">a function to test each element for a condition</param> /// <param name="append">append items in sequence to parent instance in com proxy management</param> /// <returns>true if the source sequence contains any elements; otherwise, false</returns> /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception> /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception> public static bool Any <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, bool> predicate, bool append = true) { if (source == null) { throw new ArgumentNullException("source"); } ICOMObject enumerator = null; try { enumerator = source.GetComObjectEnumerator(null); foreach (TSource current in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator)) { bool match = predicate(current); TryDispose(current); if (match) { return(true); } } if (null != enumerator && enumerator != source) { enumerator.Dispose(); } return(false); } catch { if (null != enumerator && enumerator != source) { enumerator.Dispose(); } throw; } }
private void ListInBoxFolder(Outlook.MAPIFolder inboxFolder) { // setup ui listView1.Items.Clear(); // we fetch the inbox folder items Outlook._Items items = inboxFolder.Items; ICOMObject item = null; int i = 1; do { if (null == item) { item = items.GetFirst() as ICOMObject; if (null == item) { break; } } // not every item is a mail item Outlook.MailItem mailItem = item as Outlook.MailItem; if (null != mailItem) { ListViewItem newItem = listView1.Items.Add(mailItem.SenderName); newItem.SubItems.Add(mailItem.Subject); } item.Dispose(); item = items.GetNext() as ICOMObject; i++; } while (null != item); // dipsose items and childs items.Dispose(); }
private void _excelApplication_SheetActivateEvent(ICOMObject sh) { textBoxEvents.BeginInvoke(_updateDelegate, new object[] { "Event SheetActivate called." }); sh.Dispose(); }
void ExcelApplication_SheetActivateEvent(ICOMObject Sh) { _sheetActivateEvent = true; Sh.Dispose(); }