/// <summary>Dispatch <paramref name="events"/></summary>
        /// <param name="events"></param>
        public void DispatchEvents(ref StructList12 <IEvent> events)
        {
            // Errors
            StructList4 <Exception> errors = new StructList4 <Exception>();

            for (int i = 0; i < events.Count; i++)
            {
                IEvent e = events[i];
                try
                {
                    e?.Observer?.Observer?.OnNext(e);
                }
                catch (Exception error)
                {
                    if (errorHandler != null)
                    {
                        errorHandler(this, e, error);
                    }
                    else
                    {
                        errors.Add(error);
                    }
                }
            }
            if (errors.Count > 0)
            {
                throw new AggregateException(errors.ToArray());
            }
        }
        /// <summary>
        /// Query for all token objects at path <paramref name="path"/> as type <paramref name="key"/>.
        /// </summary>
        /// <param name="path">(optional) path to query token at</param>
        /// <param name="key">(optional) key to query with</param>
        /// <param name="tokens">array of tokens, or null if failed to find matching tokens</param>
        /// <returns>true if tokens were found</returns>
        public bool TryGetAllTokens(string path, String key, out object[] tokens)
        {
            StructList4 <object[]> tokenArrays = new StructList4 <object[]>();
            int c = 0;

            foreach (var t in this.tokens)
            {
                object[] array;
                if (t.TryGetAllTokens(path, key, out array))
                {
                    c += array.Length;
                    tokenArrays.Add(array);
                }
            }
            if (tokenArrays.Count == 0)
            {
                tokens = emptyArray; return(false);
            }
            if (tokenArrays.Count == 1)
            {
                tokens = tokenArrays[0]; return(true);
            }
            object[] result = new object[c];
            int      ix     = 0;

            for (int i = 0; i < tokenArrays.Count; i++)
            {
                object[] arr = tokenArrays[i];
                Array.Copy(arr, 0, result, ix, arr.Length);
                ix += arr.Length;
            }
            tokens = result;
            return(true);
        }
        /// <summary>Union of <paramref name="o1"/> and <paramref name="o2"/>.</summary>
        public IOption Union(IOption o1, IOption o2)
        {
            StructList4 <IToken> tokens = new StructList4 <IToken>();

            if (o1 is IToken t1)
            {
                foreach (IToken t in t1.ListTokens(true))
                {
                    tokens.AddIfNew(t);
                }
            }
            if (o2 is IToken t2)
            {
                foreach (IToken t in t2.ListTokens(true))
                {
                    tokens.AddIfNew(t);
                }
            }
            if (tokens.Count == 0)
            {
                return(TokenList.NoTokens);
            }
            if (tokens.Count == 1)
            {
                return(tokens[0]);
            }
            return(new TokenList(tokens.ToArray()));
        }
        /// <summary>
        /// Get all the argument indices that are expressed in <paramref name="exp"/>.
        /// </summary>
        /// <param name="exp"></param>
        /// <param name="argumentIndices"></param>
        static void GetArguments(IExpression exp, ref StructList4 <int> argumentIndices)
        {
            StructList8 <IExpression> stack = new StructList8 <IExpression>();

            stack.Add(exp);
            while (stack.Count > 0)
            {
                IExpression e = stack.Dequeue();
                if (e is IArgumentIndexExpression arg)
                {
                    argumentIndices.AddIfNew(arg.Index);
                }
                if (e is ICompositeExpression compositeExpression)
                {
                    for (int i = 0; i < compositeExpression.ComponentCount; i++)
                    {
                        IExpression ce = compositeExpression.GetComponent(i);
                        if (ce != null)
                        {
                            stack.Add(ce);
                        }
                    }
                }
            }
        }
示例#5
0
 /// <summary>
 /// Get all canonical keys from tail towards root.
 /// </summary>
 /// <param name="line">line to read keys from</param>
 /// <param name="list">result list to write results to</param>
 /// <param name="parameterInfos">(optional) map of infos for determining if parameter is key</param>
 /// <typeparam name="LIST">List type</typeparam>
 /// <returns>dictionary of keys</returns>
 public static void GetCanonicalKeys <LIST>(this ILine line, ref LIST list, IParameterInfos parameterInfos = null) where LIST : IList <ILineParameter>
 {
     for (ILine l = line; l != null; l = l.GetPreviousPart())
     {
         if (l is ILineParameterEnumerable lineParameters)
         {
             StructList4 <ILineParameter> tmp = new StructList4 <ILineParameter>();
             foreach (var parameter in lineParameters)
             {
                 if (parameter.IsCanonicalKey(parameterInfos) && parameter.ParameterName != null && parameter.ParameterValue != null)
                 {
                     tmp.Add(parameter);
                 }
             }
             for (int i = tmp.Count - 1; i >= 0; i--)
             {
                 list.Add(tmp[i]);
             }
         }
         if (l is ILineParameter lineParameter && l.IsCanonicalKey(parameterInfos))
         {
             if (lineParameter.IsCanonicalKey(parameterInfos) && lineParameter.ParameterName != null && lineParameter.ParameterValue != null)
             {
                 list.Add(lineParameter);
             }
         }
     }
 }
示例#6
0
 /// <summary>
 /// Get all canonical keys as parameterName,parameterValue from tail towards root.
 /// </summary>
 /// <param name="line">line to read keys from</param>
 /// <param name="list">result list to write results to</param>
 /// <param name="parameterInfos">(optional) map of infos for determining if parameter is key</param>
 /// <typeparam name="LIST">List type</typeparam>
 /// <returns>dictionary of keys</returns>
 public static void GetCanonicalKeyPairs <LIST>(this ILine line, ref LIST list, IParameterInfos parameterInfos = null) where LIST : IList <KeyValuePair <string, string> >
 {
     for (ILine l = line; l != null; l = l.GetPreviousPart())
     {
         if (l is ILineParameterEnumerable lineParameters)
         {
             StructList4 <KeyValuePair <string, string> > tmp = new StructList4 <KeyValuePair <string, string> >();
             foreach (var parameter in lineParameters)
             {
                 string name = parameter.ParameterName, value = parameter.ParameterValue;
                 if (parameter.IsCanonicalKey(parameterInfos) && name != null && value != null)
                 {
                     tmp.Add(new KeyValuePair <string, string>(name, value));
                 }
             }
             for (int i = tmp.Count - 1; i >= 0; i--)
             {
                 list.Add(tmp[i]);
             }
         }
         if (l is ILineParameter lineParameter && l.IsCanonicalKey(parameterInfos))
         {
             string name = lineParameter.ParameterName, value = lineParameter.ParameterValue;
             if (lineParameter.IsCanonicalKey(parameterInfos) && name != null && value != null)
             {
                 list.Add(new KeyValuePair <string, string>(name, value));
             }
         }
     }
 }
示例#7
0
        /// <summary>
        /// Process the non-dispose. Used when <see cref="nonDisposable"/> is true (singleton instances).
        ///
        /// This may be called from <see cref="Dispose"/> or from the dispose of the last
        /// belate handle (After <see cref="Dispose"/> has been called aswell).
        ///
        /// Only one thread may process the dispose. Returns state back to 0.
        ///
        /// Unattaches all disposables, disposes them, and calls <see cref="InnerDispose(ref StructList4{Exception})"/>.
        /// Does not set state
        /// </summary>
        /// <exception cref="AggregateException">thrown if disposing threw errors</exception>
        protected virtual void ProcessNonDispose()
        {
            // Revert state
            Interlocked.CompareExchange(ref disposing, 0L, 1L);

            // Extract snapshot, clear array
            StructList2 <IDisposable> toDispose = default;

            lock (m_disposelist_lock) { toDispose = disposeList; disposeList = default; }

            // Captured errors
            StructList4 <Exception> disposeErrors = new StructList4 <Exception>();

            // Dispose disposables
            DisposeList.DisposeAndCapture(ref toDispose, ref disposeErrors);

            // Call InnerDispose(). Capture errors to compose it with others.
            try
            {
                InnerDispose(ref disposeErrors);
            }
            catch (Exception e)
            {
                // Capture error
                disposeErrors.Add(e);
            }

            // Throw captured errors
            if (disposeErrors.Count > 0)
            {
                throw new AggregateException(disposeErrors);
            }
        }
 /// <summary>Dispose called by finalizer or consumer (on Dispose())</summary>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         // Cancel source
         CancelSrc.Dispose();
         // Array of observer handles
         var handles = observers.Array;
         // Errors
         StructList4 <Exception> errors = new StructList4 <Exception>();
         foreach (var handle in handles)
         {
             try
             {
                 handle.observer.OnCompleted();
             }
             catch (Exception e)
             {
                 // Add observer exception as error event, but don't dispatch it.
                 Events.TryAdd(new OperationErrorEvent(null, e));
                 // Capture
                 errors.Add(e);
             }
         }
         if (errors.Count > 0)
         {
             throw new AggregateException(errors.ToArray());
         }
     }
 }
        /// <summary>
        /// Dispose enumerable and capture errors
        /// </summary>
        /// <param name="disposableObjects">list of disposables</param>
        /// <param name="disposeErrors">list to be created if errors occur</param>
        public static void DisposeAndCapture(IEnumerable disposableObjects, ref StructList4 <Exception> disposeErrors)
        {
            if (disposableObjects == null)
            {
                return;
            }

            // Dispose disposables
            foreach (object disposableObject in disposableObjects)
            {
                if (disposableObject is IDisposable disposable)
                {
                    try
                    {
                        disposable.Dispose();
                    }
                    catch (AggregateException ae)
                    {
                        foreach (Exception e in ae.InnerExceptions)
                        {
                            disposeErrors.Add(e);
                        }
                    }
                    catch (Exception e)
                    {
                        // Capture error
                        disposeErrors.Add(e);
                    }
                }
            }
        }
        /// <summary>Dispatch <paramref name="events"/></summary>
        /// <param name="events"></param>
        public void DispatchEvents(IEnumerable <IEvent> events)
        {
            // Errors
            StructList4 <Exception> errors = new StructList4 <Exception>();

            if (events != null)
            {
                foreach (IEvent e in events)
                {
                    try
                    {
                        e?.Observer?.Observer?.OnNext(e);
                    }
                    catch (Exception error)
                    {
                        if (errorHandler != null)
                        {
                            errorHandler(this, e, error);
                        }
                        else
                        {
                            errors.Add(error);
                        }
                    }
                }
            }
            if (errors.Count > 0)
            {
                throw new AggregateException(errors.ToArray());
            }
        }
示例#11
0
        /// <summary>
        /// Dispose observer
        /// </summary>
        /// <param name="disposeErrors"></param>
        protected override void InnerDispose(ref StructList4 <Exception> disposeErrors)
        {
            var _observer = Observer;

            // Remove watcher from dispose list.
            IFileSystem _filesystem = FileSystem;

            if (_filesystem is IDisposeList _disposelist)
            {
                _disposelist.RemoveDisposable(this);
            }

            // Call OnCompleted
            if (_observer != null)
            {
                Observer = null;
                try
                {
                    _observer.OnCompleted();
                }
                catch (Exception e)
                {
                    disposeErrors.Add(e);
                }
            }
        }
示例#12
0
        /// <summary>
        /// Add <paramref name="disposableObjects"/> to be disposed with the object.
        /// </summary>
        /// <param name="disposableObjects"></param>
        /// <returns></returns>
        bool IDisposeList.AddDisposables(IEnumerable disposableObjects)
        {
            // Argument error
            if (disposableObjects == null)
            {
                throw new ArgumentNullException(nameof(disposableObjects));
            }
            // Parent is disposed/ing
            if (IsDisposing)
            {
                // Captured errors
                StructList4 <Exception> disposeErrors = new StructList4 <Exception>();
                // Dispose now
                DisposeAndCapture(disposableObjects, ref disposeErrors);
                // Throw captured errors
                if (disposeErrors.Count > 0)
                {
                    throw new AggregateException(disposeErrors);
                }
                return(false);
            }

            // Add to list
            lock (m_disposelist_lock)
                foreach (object d in disposableObjects)
                {
                    if (d is IDisposable disposable)
                    {
                        disposeList.Add(disposable);
                    }
                }

            // Check parent again
            if (IsDisposing)
            {
                // Captured errors
                StructList4 <Exception> disposeErrors = new StructList4 <Exception>();
                // Dispose now
                DisposeAndCapture(disposableObjects, ref disposeErrors);
                // Remove
                lock (m_disposelist_lock) foreach (object d in disposableObjects)
                    {
                        if (d is IDisposable disp)
                        {
                            disposeList.Remove(disp);
                        }
                    }
                // Throw captured errors
                if (disposeErrors.Count > 0)
                {
                    throw new AggregateException(disposeErrors);
                }
                return(false);
            }

            // OK
            return(true);
        }
        /// <summary>
        /// Concatenates three tokens non-recursively.
        /// Tokens may be null valued.
        /// </summary>
        /// <param name="t1">(optional) Token</param>
        /// <param name="t2">(optional) Token</param>
        /// <param name="t3">(optional) Token</param>
        /// <returns>null, t1, t2, or concatenated token</returns>
        public static IToken Concat(this IToken t1, IToken t2, IToken t3)
        {
            StructList4 <IToken> tokens = new StructList4 <IToken>();

            if (t1 != null)
            {
                if (t1 is ITokenEnumerable enumr)
                {
                    foreach (IToken t in enumr)
                    {
                        tokens.AddIfNew(t);
                    }
                }
                else
                {
                    tokens.AddIfNew(t1);
                }
            }
            if (t2 != null)
            {
                if (t2 is ITokenEnumerable enumr)
                {
                    foreach (IToken t in enumr)
                    {
                        tokens.AddIfNew(t);
                    }
                }
                else
                {
                    tokens.AddIfNew(t2);
                }
            }
            if (t3 != null)
            {
                if (t3 is ITokenEnumerable enumr)
                {
                    foreach (IToken t in enumr)
                    {
                        tokens.AddIfNew(t);
                    }
                }
                else
                {
                    tokens.AddIfNew(t3);
                }
            }
            if (tokens.Count == 0)
            {
                return(null);
            }
            if (tokens.Count == 1)
            {
                return(tokens[0]);
            }
            return(new TokenList(tokens.ToArray()));
        }
        /// <summary>
        /// If <paramref name="tokenContainer"/> is a single token, then enumerates it.
        /// If <paramref name="tokenContainer"/> is token collection, then enumerates all contained tokens.
        /// If <paramref name="recurse"/> is true, then enumerates tree of collections.
        /// </summary>
        /// <param name="tokenContainer"></param>
        /// <param name="recurse"></param>
        /// <returns>enumerable of tokens</returns>
        public static IEnumerable <IToken> ListTokens(this IOption tokenContainer, bool recurse = true)
        {
            // Is a token
            if (tokenContainer is IToken token)
            {
                // Enumerable
                if (tokenContainer is ITokenEnumerable enumr)
                {
                    // Return enumerable as is
                    if (!recurse)
                    {
                        return(enumr);
                    }

                    // Put into queue
                    StructList4 <IToken> queue = new StructList4 <IToken>();
                    foreach (IToken t in enumr)
                    {
                        queue.Add(t);
                    }
                    StructListSorter <StructList4 <IToken>, IToken> .Reverse(ref queue);

                    StructList4 <IToken> result = new StructList4 <IToken>();
                    while (queue.Count > 0)
                    {
                        int    ix = queue.Count - 1;
                        IToken t  = queue[ix];
                        queue.RemoveAt(ix);

                        if (t is ITokenEnumerable enumr_)
                        {
                            foreach (IToken tt in enumr_)
                            {
                                queue.Add(tt);
                            }
                        }
                        else
                        {
                            result.Add(t);
                        }
                    }
                    return(result.ToArray());
                }
                // Single token
                else
                {
                    return new IToken[] { token }
                };
            }
            else
            {
                // No tokens
                return(no_tokens);
            }
        }
示例#15
0
 /// <summary>
 /// Handle dispose, forwards OnCompleted event.
 /// </summary>
 /// <param name="disposeErrors"></param>
 protected override void InnerDispose(ref StructList4 <Exception> disposeErrors)
 {
     try
     {
         Observer.OnCompleted();
     }
     catch (Exception e)
     {
         disposeErrors.Add(e);
     }
 }
示例#16
0
        /// <summary>
        /// Process the actual dispose. This may be called from Dispose() or from the dispose of the last
        /// belate handle (After Dispose() has been called aswell).
        ///
        /// Disposes all attached diposables and call <see cref="InnerDispose(ref StructList4{Exception})"/>.
        ///
        /// Only one thread may process the dispose.
        /// Sets state to 2, and then 3.
        ///
        /// Unattaches all disposables, disposes them, and calls <see cref="InnerDispose(ref StructList4{Exception})"/>.
        /// </summary>
        /// <exception cref="AggregateException">thrown if disposing threw errors</exception>
        protected virtual void ProcessDispose()
        {
            // Set state IsDisposing=2, but let only one thread continue.
            bool thisThreadChangedStateToDispose = (Interlocked.CompareExchange(ref disposing, 2L, 0L) == 0L) || (Interlocked.CompareExchange(ref disposing, 2L, 1L) == 1L);

            // Not for this thread.
            if (!thisThreadChangedStateToDispose)
            {
                return;
            }

            // Extract snapshot, clear array
            StructList2 <IDisposable> toDispose = default;

            lock (m_disposelist_lock) { toDispose = disposeList; disposeList = default; }

            // Captured errors
            StructList4 <Exception> disposeErrors = new StructList4 <Exception>();

            // Dispose disposables
            DisposeAndCapture(ref toDispose, ref disposeErrors);

            // Call InnerDispose(). Capture errors to compose it with others.
            try
            {
                InnerDispose(ref disposeErrors);
            }
            catch (Exception e)
            {
                // Capture error
                disposeErrors.Add(e);
            }

            // Call InnerDisposeUnmanaged(). Capture errors to compose it with others.
            try
            {
                InnerDisposeUnmanaged(ref disposeErrors);
            }
            catch (Exception e)
            {
                // Capture error
                disposeErrors.Add(e);
            }

            // Is disposed
            Interlocked.CompareExchange(ref disposing, 3L, 2L);

            // Throw captured errors
            if (disposeErrors.Count > 0)
            {
                throw new AggregateException(disposeErrors);
            }
        }
        /// <summary>
        /// Create a token that reads <paramref name="tokens"/> into array of tokens.
        /// Removes null values.
        /// </summary>
        /// <param name="tokens"></param>
        public TokenList(IEnumerable <IToken> tokens)
        {
            StructList4 <IToken> list = new StructList4 <IToken>();

            foreach (var t in tokens)
            {
                if (t != null)
                {
                    list.Add(t);
                }
            }
            this.tokens = list.ToArray();
        }
            /// <summary>
            /// Notify <see cref="FileSystemHttpContent"/>.
            /// </summary>
            /// <param name="disposeErrors"></param>
            protected override void InnerDispose(ref StructList4 <Exception> disposeErrors)
            {
                // Notify stream closed
                taskToNotifyOnceClosed.SetResult(Source);

                // Forward any errors from RequestTask to the caller of Stream.Close() or Stream.Dispose()
                HttpResponseMessage responseMessage = responseTask.Result;

                if (!responseMessage.IsSuccessStatusCode)
                {
                    throw new FileSystemException(filesystem, uri, responseMessage.StatusCode.ToString());
                }
            }
示例#19
0
        /// <summary>
        /// Marks dispose-has-been-called. If there are no belate handles, then proceeds with dispose.
        /// </summary>
        /// <param name="disposing">
        ///     If true, called by Dispose(), and should dispose managed and unmanaged resources.
        ///     If false, called by GC, and should dispose only unmanaged resources.
        /// </param>
        /// <exception cref="AggregateException">thrown if disposing threw errors</exception>
        protected virtual void Dispose(bool disposing)
        {
            // Called by finalized, someone forgot to dispose the object
            if (!disposing)
            {
                // Collection of errors
                StructList4 <Exception> disposeErrors = new StructList4 <Exception>();
                // Dispose unmanaged resources
                InnerDisposeUnmanaged(ref disposeErrors);
                // Throw captured errors
                if (disposeErrors.Count > 0)
                {
                    throw new AggregateException(disposeErrors);
                }
            }

            // Dispose unamnaged and managed resources
            if (disposing)
            {
                // Dispose() called
                Interlocked.CompareExchange(ref this.disposing, 1L, 0L);

                // Should dispose be started
                bool processDispose = false;

                lock (m_disposelist_lock)
                {
                    // Post-pone if there are belate handles
                    if (belateHandleCount > 0)
                    {
                        return;
                    }
                    // Set state to dispose called
                    processDispose = Interlocked.Read(ref this.disposing) <= 1L;
                }

                // Start dispose
                if (processDispose)
                {
                    if (nonDisposable)
                    {
                        ProcessNonDispose();
                    }
                    else
                    {
                        ProcessDispose();
                    }
                }
            }
        }
        /// <summary>
        /// Dispose
        /// </summary>
        public virtual void Dispose()
        {
            StructList4 <Exception> errors = new StructList4 <Exception>();

            Dispose(ref errors);
            if (errors.Count == 1)
            {
                throw new Exception(errors[0].Message, errors[0]);
            }
            if (errors.Count > 1)
            {
                throw new AggregateException(errors);
            }
        }
示例#21
0
 /// <summary>
 /// Dispose cache parts and possibly <see cref="Source"/>.
 /// </summary>
 /// <param name="errors"></param>
 protected override void Dispose(ref StructList4 <Exception> errors)
 {
     // Dispose cache parts
     base.Dispose(ref errors);
     try
     {
         // Dispose source
         if (disposeSource)
         {
             Source.Dispose();
         }
     }
     catch (Exception e)
     {
         // Add error
         errors.Add(e);
     }
 }
            /// <summary>
            /// Dispose observer
            /// </summary>
            protected override void InnerDispose(ref StructList4 <Exception> errors)
            {
                base.InnerDispose(ref errors);
                var _watcher = watcher;

                if (_watcher != null)
                {
                    watcher = null;
                    try
                    {
                        _watcher.Dispose();
                    }
                    catch (Exception e)
                    {
                        errors.Add(e);
                    }
                }
            }
 /// <summary>
 /// Dispose
 /// </summary>
 /// <param name="errors"></param>
 protected virtual void Dispose(ref StructList4 <Exception> errors)
 {
     foreach (IDisposable disposable in _getComponents <IDisposable>())
     {
         try
         {
             disposable.Dispose();
         }
         catch (Exception e)
         {
             errors.Add(e);
         }
     }
     lock (m_lock)
     {
         Clear();
         ClearCache();
     }
 }
        /// <summary>Forward events to observers.</summary>
        /// <param name="events">IEnumerable or <see cref="IEvent"/></param>
        protected void processEvents(object events)
        {
            // Errors
            StructList4 <Exception> errors = new StructList4 <Exception>();

            if (events is IEnumerable <IEvent> eventsEnumr)
            {
                foreach (IEvent e in eventsEnumr)
                {
                    try
                    {
                        e?.Observer?.Observer?.OnNext(e);
                    }
                    catch (Exception error)
                    {
                        if (errorHandler != null)
                        {
                            errorHandler(this, e, error);
                        }
                        else
                        {
                            errors.Add(error);
                        }
                    }
                }
                if (errors.Count > 0)
                {
                    throw new AggregateException(errors.ToArray());
                }
            }
            else if (events is IEvent @event)
            {
                try
                {
                    @event.Observer?.Observer?.OnNext(@event);
                }
                catch (Exception error) when(errorHandler != null)
                {
                    errorHandler(this, @event, error);
                }
            }
        }
        /// <summary>Flatten to simpler instance.</summary>
        public IOption Flatten(IOption o)
        {
            StructList4 <IToken> tokens = new StructList4 <IToken>();

            if (o is IToken t_)
            {
                foreach (IToken t in t_.ListTokens(true))
                {
                    tokens.AddIfNew(t);
                }
            }
            if (tokens.Count == 0)
            {
                return(TokenList.NoTokens);
            }
            if (tokens.Count == 1)
            {
                return(tokens[0]);
            }
            return(new TokenList(tokens.ToArray()));
        }
示例#26
0
        /// <summary>
        /// Resolve <paramref name="identifier"/> to <typeparamref name="T"/>.
        /// </summary>
        /// <param name="identifier"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns><typeparamref name="T"/> or null</returns>
        /// <exception cref="ObjectDisposedException">resolver is disposed</exception>
        /// <exception cref="LocalizationException">If resolve fails.</exception>
        public T Resolve <T>(string identifier)
        {
            StructList4 <Exception> errors = new StructList4 <Exception>();

            List <IResolver> list = resolversByType.TryGetList(typeof(T));

            if (list != null)
            {
                foreach (IResolver resolver in list)
                {
                    try
                    {
                        return(((IResolver <T>)resolver).Resolve(identifier));
                    } catch (Exception e)
                    {
                        errors.Add(e);
                    }
                }
            }

            foreach (IGenericResolver genericResolver in genericResolvers)
            {
                try
                {
                    return(genericResolver.Resolve <T>(identifier));
                }
                catch (Exception e)
                {
                    errors.Add(e);
                }
            }

            string msg = $"Failed to resolve {identifier} to {typeof(T).FullName}.";

            if (errors.Count == 0)
            {
                throw new LocalizationException(msg);
            }
            throw new LocalizationException(msg, new AggregateException(errors));
        }
        /// <summary>
        /// Dispose observer
        /// </summary>
        public void Dispose()
        {
            var _watcher  = watcher;
            var _observer = observer;

            StructList4 <Exception> errors = new StructList4 <Exception>();

            if (_observer != null)
            {
                observer = null;
                try
                {
                    _observer.OnCompleted();
                }
                catch (Exception e)
                {
                    errors.Add(e);
                }
            }

            if (_watcher != null)
            {
                watcher = null;
                try
                {
                    _watcher.Dispose();
                }
                catch (Exception e)
                {
                    errors.Add(e);
                }
            }

            if (errors.Count > 0)
            {
                throw new AggregateException(errors);
            }
            fileProvider = null;
        }
示例#28
0
            /// <summary>
            /// Write to <see cref="StringBuilder"/> <paramref name="output"/>.
            /// </summary>
            /// <param name="output"></param>
            /// <param name="format">print format</param>
            public void WriteTo(TextWriter output, Format format = Format.Default)
            {
                // Number of info fields printed
                int column = 0;

                // Print tree
                if (format.HasFlag(Format.Tree) && Level > 0)
                {
                    if (column++ > 0)
                    {
                        output.Write(" ");
                    }
                    // Print indents
                    for (int l = 1; l < Level; l++)
                    {
                        output.Write(LevelContinues(l) ? "│  " : "   ");
                    }
                    // Print last indent
                    if (Level >= 1)
                    {
                        output.Write(LevelContinues(Level) ? "├──" : "└──");
                    }
                }
                // Print name
                if (format.HasFlag(Format.Name))
                {
                    if (column++ > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write("\"");
                    output.Write(Entry.Name);
                    output.Write("\"");
                }
                // Print path
                if (format.HasFlag(Format.Path))
                {
                    if (column++ > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(Entry.Path);
                }

                // [xx, yy, zz]
                StructList4 <string> infos = new StructList4 <string>();

                // Print mountpoint
                if (format.HasFlag(Format.Mount) && Entry.IsMountPoint())
                {
                    FileSystemAssignment[] mounts = Entry.Mounts();
                    if (mounts != null)
                    {
                        for (int i = 0; i < mounts.Length; i++)
                        {
                            FileSystemAssignment info = mounts[i];
                            if (info.FileSystem != null)
                            {
                                string fs = info.FileSystem.ToString();
                                if (!String.IsNullOrEmpty(fs))
                                {
                                    infos.Add(fs);
                                }
                            }
                            if (info.Option != null)
                            {
                                string op = info.Option.ToString();
                                if (!String.IsNullOrEmpty(op))
                                {
                                    infos.Add(op);
                                }
                            }
                        }
                    }
                }

                // Write drive label [Tank]
                if (format.HasFlag(Format.DriveLabel))
                {
                    string label = Entry.DriveLabel();
                    if (!String.IsNullOrEmpty(label))
                    {
                        infos.Add(label);
                    }
                }

                // Append free space [Freespace: 10G]
                if (format.HasFlag(Format.DriveFreespace))
                {
                    long freespace = format.HasFlag(Format.DriveFreespace) ? Entry.DriveFreeSpace() : -1L;
                    if (freespace > 0)
                    {
                        infos.Add("Freespace: " + (freespace >> 30) + "G");
                    }
                }

                // Append total size [Size: 8G/32G]
                if (format.HasFlag(Format.DriveSize))
                {
                    long freespace = format.HasFlag(Format.DriveFreespace) ? Entry.DriveFreeSpace() : -1L;
                    long size      = format.HasFlag(Format.DriveSize) ? Entry.DriveSize() : -1L;
                    long reserved  = freespace < 0L ? -1L : size - freespace;
                    if (reserved > 0 && size > 0)
                    {
                        infos.Add("Size: " + (reserved >> 30) + "G/" + (size >> 30) + "G");
                    }
                    else if (size > 0)
                    {
                        infos.Add("Size: " + (size >> 30) + "G");
                    }
                }

                // Write drive type [Ram]
                if (format.HasFlag(Format.DriveType))
                {
                    DriveType driveType = Entry.DriveType();
                    if (driveType != DriveType.Unknown)
                    {
                        infos.Add(driveType.ToString());
                    }
                }

                // Write drive format [NTFS]
                if (format.HasFlag(Format.DriveFormat))
                {
                    string driveFormat = Entry.DriveFormat();
                    if (!String.IsNullOrEmpty(driveFormat))
                    {
                        infos.Add(driveFormat);
                    }
                }

                // Print file attributes
                if (format.HasFlag(Format.FileAttributes) && Entry.HasFileAttributes())
                {
                    string attribs = Entry.FileAttributes().ToString();
                    if (!string.IsNullOrEmpty(attribs))
                    {
                        infos.Add(attribs);
                    }
                }

                // Print length
                if (format.HasFlag(Format.Length) && Entry.IsFile())
                {
                    long length = Entry.Length();
                    if (length >= 0L)
                    {
                        infos.Add(length.ToString());
                    }
                }

                // Print error
                if (format.HasFlag(Format.Error) && Error != null)
                {
                    if (String.IsNullOrEmpty(Error.Message))
                    {
                        infos.Add(Error.GetType().Name);
                    }
                    else
                    {
                        infos.Add(Error.GetType().Name + ": " + Error.Message);
                    }
                }

                // Print physical path
                if (format.HasFlag(Format.PhysicalPath))
                {
                    string physicalPath = Entry.PhysicalPath();
                    if (!String.IsNullOrEmpty(physicalPath))
                    {
                        infos.Add(physicalPath);
                    }
                }

                // Print colon infos
                if (infos.Count > 0)
                {
                    if (column++ > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write('[');
                    for (int i = 0; i < infos.Count; i++)
                    {
                        if (i > 0)
                        {
                            output.Write(", ");
                        }
                        output.Write(infos[i]);
                    }
                    output.Write(']');
                }
                // Next line
                if (format.HasFlag(Format.LineFeed))
                {
                    output.WriteLine();
                }
            }
 /// <summary>Handle dispose</summary>
 protected override void InnerDispose(ref StructList4 <Exception> disposeErrors)
 {
     // Dereference client, GC will dispose it
     httpClient = null;
 }
 protected override void InnerDispose(ref StructList4 <Exception> disposeErrors)
 {
 }