Esempio n. 1
0
        /// <summary>
        ///  Returns first running com proxy, wrapped by T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="componentName"></param>
        /// <param name="className"></param>
        /// <param name="throwExceptionIfNotFound"></param>
        /// <returns></returns>
        public static T GetActiveInstance <T>(string componentName, string className, bool throwExceptionIfNotFound = false) where T : class, ICOMObject
        {
            IDisposableSequence <T> result = GetActiveInstances <T>(componentName, className);
            T item = result.FirstOrDefault();

            result.Dispose(item);
            if (throwExceptionIfNotFound && null == item)
            {
                throw new ArgumentOutOfRangeException(componentName + ", " + className);
            }
            return(item);
        }
Esempio n. 2
0
        /// <summary>
        ///  Returns first running com proxy, wrapped by T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="componentName">component name, for example Excel</param>
        /// <param name="className">class name, for example Application</param>
        /// <param name="predicate">filter predicate</param>
        /// <param name="throwExceptionIfNotFound">throw ArgumentOutOfRangeException if no instance match</param>
        /// <returns>target instance or null(Nothing in Visual Basic)</returns>
        /// <exception cref="NetOfficeCOMException">occurs if no instance match and throwExceptionIfNotFound is set</exception>
        public static T GetActiveInstance <T>(string componentName, string className, Func <T, bool> predicate, bool throwExceptionIfNotFound = false) where T : class, ICOMObject
        {
            IDisposableSequence <T> result = GetActiveInstances <T>(componentName, className, predicate);
            T item = result.FirstOrDefault();

            result.Dispose(item);
            if (throwExceptionIfNotFound && null == item)
            {
                throw new NetOfficeCOMException(String.Format("Unable to find active instance {0}.", componentName + " " + className));
            }
            return(item);
        }
Esempio n. 3
0
        /// <summary>
        ///  Returns first running com proxy, wrapped by T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate">filter predicate</param>
        /// <param name="throwExceptionIfNotFound">throw ArgumentOutOfRangeException if no instance match</param>
        /// <returns>target instance or null(Nothing in Visual Basic)</returns>
        /// <exception cref="NetOfficeException">given type doesnt have a ComProgIdAttribute annotation</exception>
        /// <exception cref="NetOfficeCOMException">occurs if no instance match and throwExceptionIfNotFound is set</exception>
        public static T GetActiveInstance <T>(Func <T, bool> predicate, bool throwExceptionIfNotFound = false) where T : class, ICOMObject
        {
            Type type      = typeof(T);
            var  attribute = type.GetCustomAttribute <ComProgIdAttribute>(true);

            if (null == attribute)
            {
                throw new NetOfficeException("Missing ComProgIdAttribute.");
            }

            string[] array = ConvertProgId(attribute.Value);
            IDisposableSequence <T> result = GetActiveInstances <T>(array[0], array[1], predicate);
            T item = result.FirstOrDefault();

            result.Dispose(item);
            if (throwExceptionIfNotFound && null == item)
            {
                throw new NetOfficeCOMException(String.Format("Unable to find active instance {0}.", array[0] + " " + array[1]));
            }
            return(item);
        }